IoTGW/WSNApiSDK/SampleCode/C
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include "SensorNetwork_Manager_API.h"
#include "AdvLog.h"
#include "test.h"
#include "AdvLog.h"
#include "AdvAPIMuxServer.h"
#if defined(WIN32) //windows
#pragma comment(lib, "SNManagerAPI.lib")
SNManagerAPI_Interface *pSNManagerAPI = NULL;
void show_menu(void)
{
printf("\n");
printf("0) Get Capability\n");
printf("1) Get Sensor Network\n");
printf("2) Set Sensor Network\n");
printf("8) Show Menu\n");
printf("9) Exit the demo\n");
printf("Enter your choice: \n");
}
static int ProcSet_Result( const char *pInJson, const int InDataLen, void *pInParam, void *pRev1 )
{
int rc = 0;
API_MUX_ASYNC_DATA *pAsynApiMux = (API_MUX_ASYNC_DATA*)pInParam;
if( pAsynApiMux->nDone == 0 ) {
snprintf(pAsynApiMux->buf, pAsynApiMux->bufsize,"%s",pInJson);
pAsynApiMux->nDone = 1;
} else if ( pAsynApiMux->nDone == -1 )
FreeMemory(pAsynApiMux);
}
return rc;
}
static int SNCALL ProceSNManagerDataCbf( const int cmdId, const char *pInJson, const int InDataLen, void **pOutParam, void *pRev1, void *pRev2 )
{
int rc = 0;
if( pInJson != NULL )
ADV_DUMP("cmdId=%d \n JSON=%s\n len=%d\n pRev1=%d\n pRev2=%d\n", cmdId, pInJson, InDataLen, pRev1, pRev2 );
switch( cmdId )
{
case SN_SetResult:
ProcSet_Result( pInJson, InDataLen, *pOutParam, pRev1 );
break;
default:
ADV_DUMP("Recv Event callback Do Nothing\n");
break;
}
return rc;
}
static void* g_dl_handle = NULL; // libAdvPlayer.so載Mu入J時E的ohandle
static int const default_argc = 3;
static char* const default_args[] = { "sampleSNManagerAPI", "-d", "3" };
int InitSNGWHandler()
{
int rc = 0;
int i = 0;
char libpath[MAX_PATH]={0};
snprintf(libpath,sizeof(libpath),"%s",SN_MANAGER_LIB_NAME);
//1. Load WSN API SDK Library
if( GetSNManagerAPILibFn( libpath,&pSNManagerAPI ) == 0 ) {
printf("Failed: InitSNGWHandler: GetSNManagerAPILibFn\r\n");
return rc;
}
// 2. SN_Manager_Initialize
if( pSNManagerAPI->SN_Manager_Initialize( ) == SN_ER_FAILED ) {
printf("Failed: InitSNGWHandler: SN_Manager_Initialize\r\n");
return rc;
}
// 3. Set ProcSNManagerCbf
if( pSNManagerAPI->SN_Manager_ActionProc( SN_Set_ReportSNManagerDataCbf, ProceSNManagerDataCbf,0,0,0) == SN_ER_FAILED ){
printf("Failed: InitSNGWHandler: SN_Set_ReportSNManagerDataCbf\r\n");
return rc;
}
rc = 1;
printf("Success: Initial Sensor Network Handler\r\n");
return rc;
}
int getText(char *variable, int size){
if(fgets(variable, sizeof(char) * size, stdin) != NULL ) {
sscanf(variable, "%[^\n]", variable);
return strlen(variable);
}
return 0;
}
int main(int argc, char *argv[] )
{
int rc = 0;
int count = 0;
int i = 0;
int result, done, op;
char num[12];
char pwd[128];
char libpath[512];
char szRESTfulAPICmd[512]={0};
char szValue[128]={0};
AdvLog_Init();
if (argc == 1) // no arguments were passed
{
// do things for no arguments
// usually those variables are set here for a generic flow onwards
argc = default_argc;
argv = default_args;
}
AdvLog_Arg(argc,argv);
if( InitSNGWHandler() == 0 )
goto ExitSample;
InitAdvAPIMux_Server();
done = 0;
while (! done) {
show_menu();
op = -1;
if( getText(num,12) > 0 ) {
if(isdigit(num[0])) {
op = atoi(num);
}
}
if( op == -1 ) {
printf("Please input a number\n");
continue;
}
memset(szRESTfulAPICmd,0,sizeof(szRESTfulAPICmd));
memset(szValue,0,sizeof(szValue));
switch (op) {
case 0: // GET Capability
printf("Get Capability Result= %s \n", pSNManagerAPI->SN_Manager_GetCapability( ) );
break;
case 1: // GET
printf("Please input Get API Command\r\n");
printf("ex: SenHub/SenData/co\n");
printf("ex: SenHub/Info/Name\n");
if( getText(szRESTfulAPICmd,sizeof(szRESTfulAPICmd)) <= 0 ) {
result = -1;
printf("Input API Command Error\n");
break;
}
printf("Get RESTful API= %s Result= %s \n", szRESTfulAPICmd, pSNManagerAPI->SN_Manager_GetData(szRESTfulAPICmd, CACHE_MODE ));
break;
case 2: // SET
printf("Please input Set API Command\r\n");
printf("ex: SenHub/SenData/dout\n");
printf("ex: SenHub/Info/Name\n");
if( getText(szRESTfulAPICmd,sizeof(szRESTfulAPICmd)) <= 0 ) {
result = -1;
printf("Input API Command Error\n");
break;
}
printf("Please input Set Value in JSON Format\r\n");
printf("ex: {\"bv\":1}\n");
printf("ex: {\"sv\":\"Mote1\"}\n");
if( getText(szValue,sizeof(szValue)) <= 0 ) {
result = -1;
printf("Input setting value Error\n");
break;
}
printf("Set RESTful API=%s Value=%s Result=%s\r\n", szRESTfulAPICmd, szValue, pSNManagerAPI->SN_Manager_SetData( szRESTfulAPICmd, szValue, NULL));
break;
case 3: // Get Version
pSNManagerAPI->SN_Manager_GetVersion(szValue, 128 );
printf("Sensor Network Manager API Version is=%s", szValue );
break;
case 9:
done = 1;
break;
case 8: // show menum again
show_menu( );
break;
default:
printf("\nUnknown choice!\n\n");
continue;
}
}
// Release the WSN API SDK
pSNManagerAPI->SN_Manager_Uninitialize();
UnInitAdvAPIMux_Server();
ExitSample:
printf("Exit SNManager Sample\n");
return 0;
}