#include <stdlib.h> #include <unistd.h>
struct conf_info { int pid; char name[128]; char pubkey_file[128]; char guid_file[128]; char license_file[128]; }; struct conf_info configInfo = {0, {0}, {0}, {0}, {0}};
bool readConfigFromFile(void) { char buf[128]={0}; char *line = NULL; size_t len = 0; ssize_t read; bool ret = true; char *pconf[] = {configInfo.pubkey_file, configInfo.guid_file, configInfo.license_file}; FILE * file = fopen("./conf/config", "rb"); if (!file) { printf("open ./conf/config failed...\n"); return false; } read = getline(&line, &len, file); if (read > sizeof(buf)) { ret = false; printf("read PID failed...\n"); } else { strncpy(buf,line,read-1); buf[read]='\0'; configInfo.pid = atoi(buf); printf("PID=%d\n",configInfo.pid); } read = getline(&line, &len, file); if (read > sizeof(buf)) { ret = false; printf("read Name failed...\n"); } else { strncpy(buf,line,read-1); buf[read]='\0'; sprintf(configInfo.name, buf); } int i; for (i=0; i<sizeof(pconf)/sizeof(char *); i++) { read = getline(&line, &len, file); if (read > sizeof(buf)) { ret = false; printf("read line%d failed...\n", i+2); break; } else { strncpy(buf,line,read-1); buf[read]='\0'; if (access(buf, R_OK)) { ret = false; printf("fail to read %s\n",buf); break; } else { sprintf(pconf[i], buf); } } } if (ret) { printf("NAME=%s\n",configInfo.name); printf("PEM =%s\n",configInfo.pubkey_file); printf("GUID=%s\n",configInfo.guid_file); printf("LICENSE=%s\n",configInfo.license_file); } if (line) free(line); fclose(file); return ret; }
bool initDevice() { + if(readConfigFromFile() == false){ + return false; + }
unsigned char license[256] = {0}; int nLicenseSize = 0; - if (!readBufferFromFile("./licence.sign.file.txt", license, sizeof(license), &nLicenseSize)) { + if (!readBufferFromFile(configInfo.license_file, license, sizeof(license), &nLicenseSize)) {
- if(!readBufferFromFile("./GUID_file.txt", guid, sizeof(guid), &nGUIDSize)) { + if (!readBufferFromFile(configInfo.guid_file, guid, sizeof(guid), &nGUIDSize)) {
- if (!readBufferFromFile("./1000000004.pem", svrPubkey, sizeof(svrPubkey), &nPubkeySize)) + if (!readBufferFromFile(configInfo.pubkey_file, svrPubkey, sizeof(svrPubkey), &nPubkeySize))
- info.device_name = "demo1"; + info.device_name = configInfo.name; info.device_serial_number = guid; info.device_license = license; info.product_version = 1; info.network_type = network_type_wifi; - info.product_id = 1000000004; + info.product_id = configInfo.pid; info.server_pub_key = svrPubkey; }
|