diff --git a/Example/main.c b/Example/main.c index 0e57a87..b087638 100644 --- a/Example/main.c +++ b/Example/main.c @@ -25,6 +25,7 @@ #include "skins_res.h" #include "server_addr.h" #include "boardlink_iot.h" +#include "mijia_iot.h" static MOD_INFO_TABLE g_ModInfo; static uv_timer_t g_tmTest; @@ -1919,14 +1920,17 @@ int main(int argc, char **argv) uv_loop_t* pLoop = GetDBusDefaultLoop(); int modIdx = -1; char buf[256]; + char base64code[] = {0xCE, 0xDE, 0xCF, 0xDF, 0x57, 0x69, 0x46, 0x69, 0xC4, 0xC4, 0xBC, + 0xD2, 0xC7, 0xBF, 0xB0, 0xA1, 0xC0, 0xB6, 0xCF, 0xE8}; - +#if 0 struct tm tm; memset(&tm, 0, sizeof(struct tm)); strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm); strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm); puts(buf); +#endif #if 0 i = GetServerModeFromCC(PUBLISH_MODE, &ret); @@ -2001,7 +2005,20 @@ int main(int argc, char **argv) return 0; } + LOG_EX(LOG_Debug, "Base64:[%s]\n", EvpBase64Encode(base64code)); + SetHBLAutoExit(TRUE); + + if(modIdx == 20) + { +#ifndef PLATFORM_CPU + if(MijiaIoT_Init("11672758", "DH5Z7KOWWsqdwW5J") == 0) + { + MijiaIoT_ServerStart(); + MijiaIoT_UnBindDevice(); + } +#endif + } //SysPointMarkInit(NULL, -1, -1); //test_task_new(__uvThreadSysPointUpload, NULL); diff --git a/Framework/IoT/Mijia/mijia_iot.c b/Framework/IoT/Mijia/mijia_iot.c new file mode 100644 index 0000000..401c93d --- /dev/null +++ b/Framework/IoT/Mijia/mijia_iot.c @@ -0,0 +1,199 @@ +#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 diff --git a/Framework/Skins/skins.c b/Framework/Skins/skins.c index d44b51f..c6e9320 100644 --- a/Framework/Skins/skins.c +++ b/Framework/Skins/skins.c @@ -354,7 +354,7 @@ static int __skinCreateCfgFile(const char* pCfgFilePath) int rc = 0; static sqlite3* pSqlFileDB = NULL; UT_string *pSqlCmd = NULL; -#if 0 + rc = sqlite3_open(":memory:", &g_pMemDb); if(rc != SQLITE_OK) @@ -377,8 +377,8 @@ static int __skinCreateCfgFile(const char* pCfgFilePath) utstring_free(pSqlCmd); return -ERR_SQLITE3_CREATE_TABLE; } -#endif - rc = sqlite3_open_v2(pCfgFilePath, &pSqlFileDB, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE , NULL); + + rc = sqlite3_open_v2(pCfgFilePath, &pSqlFileDB, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if(rc != SQLITE_OK) { @@ -388,8 +388,6 @@ static int __skinCreateCfgFile(const char* pCfgFilePath) return -ERR_OPEN_SQLITE3_DB; } - LOG_EX(LOG_Debug, "Create Database: %s\n", pCfgFilePath); -#if 0 utstring_renew(pSqlCmd); utstring_printf(pSqlCmd, CREATE_SKIN_TBL_SQL""CREATE_RES_TBL_SQL, "", ""); @@ -404,37 +402,7 @@ static int __skinCreateCfgFile(const char* pCfgFilePath) utstring_free(pSqlCmd); return -ERR_SQLITE3_CREATE_TABLE; } -#else - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, CREATE_SKIN_TBL_SQL, ""); - rc = sqlite3_exec(pSqlFileDB, utstring_body(pSqlCmd), NULL, 0, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl \n[%s]\n Error(%d): %s\n", utstring_body(pSqlCmd), rc, pErrMsg); - sqlite3_free(pErrMsg); - unlink(pCfgFilePath); - sqlite3_close(g_pMemDb); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, CREATE_RES_TBL_SQL, ""); - - rc = sqlite3_exec(pSqlFileDB, utstring_body(pSqlCmd), NULL, 0, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl \n[%s]\n Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - unlink(pCfgFilePath); - sqlite3_close(g_pMemDb); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } -#endif utstring_renew(pSqlCmd); utstring_printf(pSqlCmd, UPGRADE_TBL_SQL_CMD, ""); rc = sqlite3_exec(pSqlFileDB, utstring_body(pSqlCmd), NULL, 0, &pErrMsg); diff --git a/Framework/libuvEngine/libcomm.c b/Framework/libuvEngine/libcomm.c index 141e7fb..c49dcc4 100644 --- a/Framework/libuvEngine/libcomm.c +++ b/Framework/libuvEngine/libcomm.c @@ -15,6 +15,20 @@ #include "log.h" #include "libuv_dbus.h" +int IsFileExists(const char* pPath) +{ + struct stat st; + + if (stat(pPath, &st) != 0) + { + return FALSE; + } + else + { + return TRUE; + } +} + int CopyFile(const char *pSrc, const char *pDest) { int fdSrc, fdDest; diff --git a/include/libuv_dbus.h b/include/libuv_dbus.h index e019b8e..8f8d03e 100644 --- a/include/libuv_dbus.h +++ b/include/libuv_dbus.h @@ -224,6 +224,7 @@ char* CfgGetStringValue(const char* pTags, char* pDefValue); double CfgGetFloatValue(const char* pTags, double defValue); int CfgGetBoolValue(const char* pTags, int defValue); void SetHBLAutoExit(int flag); +int IsFileExists(const char* pPath); extern char *strptime(const char *s, const char *format, struct tm *tm); #ifdef __cplusplus diff --git a/include/mijia_iot.h b/include/mijia_iot.h new file mode 100644 index 0000000..387db81 --- /dev/null +++ b/include/mijia_iot.h @@ -0,0 +1,10 @@ +#ifndef MIJIA_IOT_H +#define MIJIA_IOT_H + + +int MijiaIoT_Init(const char* pDid, const char* pKey); +int MijiaIoT_ServerStart(void); +int MijiaIoT_ServerReStart(void); +int MijiaIoT_ServerStop(void); +int MijiaIoT_UnBindDevice(void); +#endif \ No newline at end of file