#ifndef PLATFORM_CPU #include #include #include #include #include #if defined(PLATFORM_R16) || defined(PLATFORM_R311) #include "log.h" #include "libuv_dbus.h" #include "mijia_iot.h" #else #include #include #include #endif #define GET_DID_CMD ("cat /mnt/UDISK/miio/device.conf | grep did= | cut -d \"=\" -f2") #define GET_KEY_CMD ("cat /mnt/UDISK/miio/device.conf | grep key= | cut -d \"=\" -f2") #define GET_MAC_CMD ("cat /mnt/UDISK/miio/device.conf | grep mac= | cut -d \"=\" -f2") #define GET_WLAN_MAC_CMD ("ifconfig wlan0 | grep HWaddr | awk \'{print $5}\'") #define MIJIA_CFG_FILE ("/mnt/UDISK/miio/device.conf") #define MIJIA_BIND_FILE ("/mnt/UDISK/miio/device.token") #define MIJIA_SVR_FILE "/usr/bin/miio_client" #define MIJIA_CFG_FMT "# did must be a unsigned int\n"\ "# key must be a string\n" \ "#\n" \ "did=%s\n" \ "key=%s\n" \ "vendor=163\n" \ "mac=%s\n" \ "# model max len 23\n" \ "model=163.wifispeaker.a1\n" static int __mijia_InitCfgFile(const char* pDid, const char* pKey, char* pMacAddr) { char* pCfgContent = NULL; FILE* pFile = NULL; if(pKey == NULL || strlen(pKey) == 0) { LOG_EX(LOG_Error, "Error Input pKey params: %s\n", SAFE_STRING_VALUE(pKey)); return -ERR_INPUT_PARAMS; } if(pDid == NULL || strlen(pDid) == 0) { LOG_EX(LOG_Error, "Error Input pDid params: %s\n", SAFE_STRING_VALUE(pDid)); return -ERR_INPUT_PARAMS; } pFile = fopen(MIJIA_CFG_FILE, "w+"); if(pFile == NULL) { LOG_EX(LOG_Error, "Create File %s Error\n", MIJIA_CFG_FILE); return -ERR_CREATE_CFG_FILE; } pCfgContent = malloc(1024); if(pCfgContent == NULL) { fclose(pFile); LOG_EX(LOG_Error, "Malloc Error\n"); return -ERR_MALLOC_MEMORY; } sprintf(pCfgContent, MIJIA_CFG_FMT, pDid, pKey, pMacAddr); fwrite(pCfgContent, 1, strlen(pCfgContent), pFile); LOG_EX(LOG_Debug, "Create Mijia Configure File:\n%s\n", pCfgContent); fclose(pFile); free(pCfgContent); return 0; } int MijiaIoT_Init(const char* pDid, const char* pKey) { int ret = 0; char* pMacAddr = NULL; char* pCfgDid = NULL; char* pCfgKey = NULL; ret = GetShellExecResult(GET_WLAN_MAC_CMD, &pMacAddr); if(ret != 0 || pMacAddr == NULL) { LOG_EX(LOG_Error, "Run Cmd [%s] Error: %d\n", GET_WLAN_MAC_CMD, ret); return -ERR_READ_FILE; } if(strlen(pMacAddr) == 0) { LOG_EX(LOG_Error, "Get Mac Error: %s\n", pMacAddr); free(pMacAddr); return -ERR_NO_ITEMS; } if(!IsFileExists(MIJIA_CFG_FILE)) { __mijia_InitCfgFile(pDid, pKey, pMacAddr); free(pMacAddr); return 0; } ret = GetShellExecResult(GET_DID_CMD, &pCfgDid); if(ret != 0 || pCfgDid == NULL) { LOG_EX(LOG_Error, "Run Cmd [%s] Error: %d\n", GET_DID_CMD, ret); return -ERR_READ_FILE; } ret = GetShellExecResult(GET_KEY_CMD, &pCfgKey); if(ret != 0 || pCfgKey == NULL) { LOG_EX(LOG_Error, "Run Cmd [%s] Error: %d\n", GET_KEY_CMD, ret); return -ERR_READ_FILE; } LOG_EX(LOG_Debug, "Mijia Configure:\n\tDid = %s\n\tKey = %s\n\tMac = %s\n", pCfgDid, pCfgKey, pMacAddr); free(pMacAddr); free(pCfgDid); free(pCfgKey); return 0; } int MijiaIoT_ServerStart(void) { const char* pSvrCmd = "/etc/init.d/mijia_services start"; if(IsFileExists(MIJIA_SVR_FILE)) { LOG_EX(LOG_Debug, "Run Mijia Server:[%s]\n", pSvrCmd); system(pSvrCmd); return 0; } else { LOG_EX(LOG_Error, "Can't Find Mijia Server: %s\n", MIJIA_SVR_FILE); return -ERR_FILE_NOT_EXISTS; } } int MijiaIoT_ServerStop(void) { const char* pSvrCmd = "/etc/init.d/mijia_services stop"; if(IsFileExists(MIJIA_SVR_FILE)) { LOG_EX(LOG_Debug, "Run Mijia Server:[%s]\n", pSvrCmd); system(pSvrCmd); return 0; } else { LOG_EX(LOG_Error, "Can't Find Mijia Server: %s\n", MIJIA_SVR_FILE); return -ERR_FILE_NOT_EXISTS; } } int MijiaIoT_ServerReStart(void) { const char* pSvrCmd = "/etc/init.d/mijia_services restart"; if(IsFileExists(MIJIA_SVR_FILE)) { LOG_EX(LOG_Debug, "Run Mijia Server:[%s]\n", pSvrCmd); system(pSvrCmd); return 0; } else { LOG_EX(LOG_Error, "Can't Find Mijia Server: %s\n", MIJIA_SVR_FILE); return -ERR_FILE_NOT_EXISTS; } } int MijiaIoT_UnBindDevice(void) { if(IsFileExists(MIJIA_BIND_FILE)) { LOG_EX(LOG_Debug, "Remove Device Bind\n"); unlink(MIJIA_BIND_FILE); } return 0; } #endif