WISE-PaaS/OTA Agent(Android)

From ESS-WIKI
Jump to: navigation, search

Introduction

This document demonstrates how to support our ota-agent solution under your android system.This demonstration is android 4.3 based. If you encounter problems under your some-verson android system,please mailto me: jin.xin@advantech.com.cn

Architecture

RTENOTITLE

The android ota solution is compose of two parts: the C-lanuage  based Ota-agent and the Java-language based OtaService.

  • Ota-agent achieves communiction with the ota-server, besides, it achieves the package download and unzip. But, the Ota-agent is C-language based and it can't invoke the Java-language based android OTA APIs, so the OtaService comes.
  • OtaService is on the android framework layer, so it can invoke the android OTA APIs directly. The OtaService provides as a socket server. So the Ota-agent can invoke the android OTA APIs through the socket connnect providing by OTaService indirectly.

Android OtaService

we now support four commands, you can  extent your commands:

private void startTestThread(final LocalSocket socket){
	Thread t = new Thread(){
		@Override 
		public void run(){
			try{
				InputStream is = socket.getInputStream();
				InputStreamReader isr = new InputStreamReader(is);
				while(true){//
					char [] data = new char[128];
					int readBytes = isr.read(data);
					if(readBytes != -1){
						String tempStr = new String(data, 0, readBytes);
						Slog.i(TAG, "recv str: " + tempStr + " len is :" + readBytes);
						String newstr = tempStr.trim();
						Slog.i(TAG, "newstr: " + newstr);
						String[] asplit = newstr.split("#");
						Slog.i(TAG, "asplit[0]: " + asplit[0]);
						//RecoverySystem.rebootWipeCache(mContext);
						switch(Advaction.getAdvaction(asplit[0])){	
						case systembackup:
							Slog.i(TAG, "switch systembackup");
							RecoverySystem.rebootSystemBackup(mContext);
							break;
						case wipedata:
							Slog.i(TAG, "switch wipedata");
							RecoverySystem.rebootWipeUserData(mContext);
							break;
						case wipecache:
							Slog.i(TAG, "swtich wipecache");
							RecoverySystem.rebootWipeCache(mContext);
							break;
						case install:
							Slog.i(TAG, "switch install");
							String file = asplit[1].trim();
							Slog.i(TAG, "install file: " + file);
							File recoveryFile = new File(file);
                					if(!recoveryFile.exists()){
								Slog.i(TAG, "install, file not exist");
								return;
							}
							RecoverySystem.installPackage(mContext, recoveryFile);
							break;
						case appinstall:
							Slog.i(TAG, "switch appinstall");
                                                        String strfile = asplit[1].trim();
                                                        String newVersionName, newpackageName;
                                                        String oldVersionName = null;
							int  newVersionCode = 0, oldVersionCode = 0;
						    	int isinstall = 0;	
							//get apk info 
							PackageInfo info = mPackageManager.getPackageArchiveInfo(strfile, PackageManager.GET_ACTIVITIES);
							if(info != null){
								ApplicationInfo appInfo = info.applicationInfo;
								newpackageName = appInfo.packageName;
								newVersionName = info.versionName;
								newVersionCode = info.versionCode;
								Slog.i(TAG, "ApkName: "+newpackageName+"; versionName: "+newVersionName+"; versionCode: "+Integer.toString(newVersionCode));	
								
								//get installed package info
                                                        	List<PackageInfo> packages = mPackageManager.getInstalledPackages(0);
                                                       	 	for(int i=0;i<packages.size();i++) {
                                                                	PackageInfo packageInfo = packages.get(i);
                                                               	 	AppInfo tmpInfo =new AppInfo();
                                                                	tmpInfo.appName = packageInfo.applicationInfo.loadLabel(mPackageManager).toString();
                                                                	tmpInfo.packageName = packageInfo.packageName;
                                                                	tmpInfo.versionName = packageInfo.versionName;
                                                                	tmpInfo.versionCode = packageInfo.versionCode;
                                                                	Slog.i(TAG, "Name: "+tmpInfo.packageName+"; versionName: "+tmpInfo.versionName+"; versionCode: "+Integer.toString(tmpInfo.versionCode));
                                                        		if(tmpInfo.packageName.equals(newpackageName)){
										oldVersionName = tmpInfo.versionName;
										oldVersionCode = tmpInfo.versionCode;
										break;
									}
								}
                                                        	//get installed package info end
								if(oldVersionName == null){
									Slog.i(TAG, "get installed apk info failed, this apk may not installed before");
									isinstall = 1;
								}
							}
							else{
								Slog.i(TAG, "getPackageArchiveInfo failed, may be invalid apk file");
								return;
							}
							//get apk info end
						
							Slog.i(TAG, "install apk name: " + strfile);
							File apkfile = new File(strfile);
							if(isinstall == 1 || newVersionCode >= oldVersionCode){
								//truely need install the apk
								Slog.i(TAG, "now begin install the apk "+strfile);
								mPackageManager.installPackage(Uri.fromFile(apkfile), null, PackageManager.INSTALL_REPLACE_EXISTING, null);
							}
							break;
						default:
							Slog.i(TAG, "error paramer");
							break;		
						}
					}
					else
						Slog.i(TAG, "recv error");
					Slog.i(TAG, "finished");
					return;
				}
			}catch (IOException e){
				e.printStackTrace();	
			}
			
		}
	};
	t.start();
}
Get source code from here: https://github.com/ADVANTECH-Corp/wise-paas-ota-agent-android

Android OtaService test tools

To test the OtaService, I developed a OtaClient test tools:

void usage()
{
#define ALOGI printf
	ALOGI(LOG_TAG "not support, usage: \n");
        ALOGI(LOG_TAG "servertest [action string] [option]\n");
        ALOGI(LOG_TAG "action string can be:\n");
        ALOGI(LOG_TAG "    systembackup -- mean system backup\n");
        ALOGI(LOG_TAG "    wipedata -- mean wipe data partition\n");
        ALOGI(LOG_TAG "    wipecache -- mean wipe cache partition\n");
        ALOGI(LOG_TAG "    install /cache/update.zip -- means update system and /cache/update.zip is the option\n");
        ALOGI(LOG_TAG "    appinstall /cache/test.apk -- means install/reinstall app and /cache/test.apk is the option\n");
}

int main(const int argc, const char *argv[]) {
    char buf[] = "systembackup";//for system backup
    //char buf[] = "install /cache/update.zip";
    //char buf[] = "wipecache";
    //char buf[] = "wipecache";
    int len = strlen(buf);
    static int clientfd;

    ALOGI(LOG_TAG "test recovery functions...\n");
    if(argc <= 1 && argc > 3){
        usage();
	return -1;
    }
    else if(argc == 2){
        if(strncmp(argv[1], "systembackup", strlen("systembackup")) && strncmp(argv[1], "wipedata", strlen("wipedata")) && strncmp(argv[1], "wipecache", strlen("wipecache"))){
                usage();
                return -1;
        }
    }
    else if(argc == 3){
        if(strncmp(argv[1], "install", strlen("install")) && strncmp(argv[1], "appinstall", strlen("appinstall"))){
                usage();
                return -1;
        }
    }
    else{
	usage();
	return -1;
    }

    
    clientfd = socket_local_client("ota_server", ANDROID_SOCKET_NAMESPACE_ABSTRACT,SOCK_STREAM);
    if(clientfd < 0){
	ALOGE(LOG_TAG "Failed to connect to local socket test_server: %s\n", strerror(errno));
	exit(1);
    }
    ALOGI(LOG_TAG "test connect successful\n");
    
    sleep(1);	    
    if(argc == 2){
	writex(clientfd, argv[1], strlen(argv[1]));
    }
    else if(argc == 3){
	char astr[1024] = {0};
	int len1 = strlen(argv[1]);
	int len2 = strlen(argv[2]);
	int len3 = strlen("#");
	strncpy(astr, argv[1], len1);
	strncpy(astr+len1, "#", len3);
	strncpy(astr+len1+len3, argv[2], len2);
	writex(clientfd, astr, strlen(astr));	
    }
    else{
    	usage();
    	return -1;
    }
    return 0;
}
Get source code from here: https://github.com/ADVANTECH-Corp/wise-paas-ota-agent-android

Android Ota-agent

The Android Ota-agent is same as the wise-pass ota-agent,The difference is that I modify the linux's Makefie to Android's Android.mk.

You can get the binary file for android from here:

https://github.com/ADVANTECH-Corp/wise-paas-ota-agent-android

Or you can rebuild the ota-agent under android system too.