void allParameters ( common_EnumExample_t a, uint32_t* bPtr, const uint32_t* dataPtr, size_t dataNumElements, uint32_t* outputPtr, size_t* outputNumElementsPtr, const char* label, char* response, size_t responseNumElements, char* more, size_t moreNumElements ) { int i; // Print out received values LE_PRINT_VALUE("%i", a); LE_PRINT_VALUE("%s", label); LE_PRINT_ARRAY("%i", dataNumElements, dataPtr); // Generate return values *bPtr = a; for (i=0; i<*outputNumElementsPtr; i++) { outputPtr[i] = i*a; } le_utf8_Copy(response, "response string", responseNumElements, NULL); le_utf8_Copy(more, "more info", moreNumElements, NULL); }
void CallbackTestHandler ( uint32_t data, const char* namePtr, int dataFile, void* contextPtr ) { LE_PRINT_VALUE("%d", data); LE_PRINT_VALUE("'%s'", namePtr); LE_PRINT_VALUE("%p", contextPtr); LE_PRINT_VALUE("%i", dataFile); // Read and print out whatever is read from the dataFile fd. writeFdToLog(dataFile); // This should fail, because the callback can only be called once. LE_DEBUG("Triggering CallbackTest second time -- should FATAL"); TriggerCallbackTest(257); // Continue with the next test // todo: can't continue because the previous test fails -- maybe need to separate tests //le_event_QueueFunction(testFinal, NULL, NULL); }
void test3(void) { uint32_t value=5; uint32_t data[] = {3, 9, 4, 1}; size_t length=14; uint32_t output[length]; char response[21]; char more[21]; allParameters(COMMON_THREE, &value, data, 4, output, &length, "new thread string", response, sizeof(response), more, sizeof(more)); LE_PRINT_VALUE("%i", value); LE_PRINT_ARRAY("%i", length, output); LE_PRINT_VALUE("%s", response); LE_PRINT_VALUE("%s", more); }
static void HandleTestA ( int32_t x, void* contextPtr ) { static int count=0; count++; LE_PRINT_VALUE("%i", x); if ( contextPtr == ClientMessage ) { LE_DEBUG("HandleTestA: context pointer works"); LE_PRINT_VALUE( "'%s'", (char *)contextPtr ); } else { LE_DEBUG("HandleTestA: context pointer fails"); } // Re-do the test again for the given number of times. if ( count < 2 ) { banner("Test 2 again"); LE_PRINT_VALUE("%i", count); HandlerRef = AddTestAHandler(HandleTestA, (void*)ClientMessage); LE_PRINT_VALUE("%p", HandlerRef); LE_DEBUG("Triggering TestA yet again for count=%i\n", count); TriggerTestA(); } }
void test1(void) { uint32_t value=10; uint32_t data[] = {1, 2, 3, 4}; size_t length=10; uint32_t output[length]; char response[21]; char more[21]; allParameters(COMMON_TWO, &value, data, 4, output, &length, "input string", response, sizeof(response), more, sizeof(more)); LE_PRINT_VALUE("%i", value); LE_PRINT_ARRAY("%i", length, output); LE_PRINT_VALUE("%s", response); LE_PRINT_VALUE("%s", more); }
void test3(void) { banner("Test 3"); // Test what happens if an event is triggered, then the handler is removed. The registered // handler should not be called, even if there is a pending event, because the handler has // been removed. TestAHandlerRef_t handlerRef = AddTestAHandler(NewHandleTestA, NULL); LE_PRINT_VALUE("%p", handlerRef); LE_DEBUG("Triggering New TestA\n"); TriggerTestA(); RemoveTestAHandler(handlerRef); // Test function callback parameters. int result; // This is not used in the test; this parameter was added to test a code generation bug fix. uint8_t dataArray[] = { 1, 2 }; result = TestCallback(10, dataArray, 2, CallbackTestHandler, NULL); LE_PRINT_VALUE("%d", result); LE_DEBUG("Triggering CallbackTest"); TriggerCallbackTest(100); // Need to allow the event loop to process the trigger. // The rest of the test will be continued in the handler. }
static void HandleTestA ( int32_t x, void* contextPtr ) { LE_PRINT_VALUE("%i", x); if ( contextPtr == &SomeData ) { LE_DEBUG("HandleTestA: context pointer works"); LE_PRINT_VALUE( "%u", *((uint32_t*)contextPtr) ); } else { LE_DEBUG("HandleTestA: context pointer fails"); } // continue the rest of the test LE_DEBUG("Removing TestA"); RemoveTestAHandler(HandlerRef); LE_DEBUG("Triggering TestA again"); TriggerTestA(); // Continue with next test test3(); }
void FileTest ( int dataFile, int* dataOutPtr ) { // Read and print out whatever is read from the client fd char buffer[1000]; ssize_t numRead; numRead = read(dataFile, buffer, sizeof(buffer)); buffer[numRead] = '\0'; LE_PRINT_VALUE("%zd", numRead); LE_PRINT_VALUE("%s", buffer); // Open a known file to return back to the client *dataOutPtr = open("/usr/include/stdio.h", O_RDONLY); LE_PRINT_VALUE("%i", *dataOutPtr); // Read a bit from the file, to make sure it is okay numRead = read(*dataOutPtr, buffer, sizeof(buffer)); buffer[numRead] = '\0'; LE_PRINT_VALUE("%zd", numRead); LE_PRINT_VALUE("%s", buffer); }
//-------------------------------------------------------------------------------------------------- static void ProcessCommand ( void* reportPtr ) { uint32_t command = *(uint32_t*)reportPtr; LE_PRINT_VALUE("%i", command); if (command == REQUEST_COMMAND) { RequestCount++; StartCellularNetwork(); } else if (command == RELEASE_COMMAND) { // Don't decrement below zero, as it will wrap-around. if ( RequestCount > 0 ) { RequestCount--; } if (RequestCount == 0) { StopCellularNetwork(); } } else { LE_ERROR("Command %i is not valid", command); } }
//-------------------------------------------------------------------------------------------------- static void VerifyPollingTimer ( uint32_t testValue ) { le_result_t rc; uint32_t pollingTimer; rc = le_avc_SetPollingTimer(testValue); if (rc != LE_OK) { LE_ERROR("Failed reading polling timer."); } rc = le_avc_GetPollingTimer(&pollingTimer); if (rc != LE_OK) { LE_ERROR("Failed reading polling timer."); } LE_PRINT_VALUE("%d", pollingTimer); if (pollingTimer != testValue) { LE_ERROR("Polling Timer test failed."); } }
//-------------------------------------------------------------------------------------------------- void le_avdata_ReleaseSession ( le_avdata_RequestSessionObjRef_t sessionRequestRef ) { le_ref_IterRef_t iterRef; // Look up the reference. If it is NULL, then the reference is not valid. // Otherwise, delete the reference and request avcServer to release session. void* sessionPtr = le_ref_Lookup(AvSessionRequestRefMap, sessionRequestRef); if (sessionPtr == NULL) { LE_ERROR("Invalid session request reference %p", sessionPtr); return; } else { LE_PRINT_VALUE("%p", sessionPtr); le_ref_DeleteRef(AvSessionRequestRefMap, sessionRequestRef); } // Stop the session when all clients release their session reference. iterRef = le_ref_GetIterator(AvSessionRequestRefMap); if (le_ref_NextNode(iterRef) == LE_NOT_FOUND) { // Close the session if there is no new open request for 2 seconds. le_timer_Restart(SessionReleaseTimerRef); } }
void writeFdToLog(int fd) { char buffer[1000]; ssize_t numRead; numRead = read(fd, buffer, sizeof(buffer)); if (numRead == -1) { LE_INFO("Read error: %s", strerror(errno)); } else { buffer[numRead] = '\0'; LE_PRINT_VALUE("%zd", numRead); LE_PRINT_VALUE("%s", buffer); } }
static void NewHandleTestA ( int32_t x, void* contextPtr ) { LE_PRINT_VALUE("%i", x); }
//-------------------------------------------------------------------------------------------------- void le_cellnet_RemoveStateEventHandler ( le_cellnet_StateEventHandlerRef_t addHandlerRef ) { LE_PRINT_VALUE("%p", addHandlerRef); le_event_RemoveHandler((le_event_HandlerRef_t)addHandlerRef); }
//-------------------------------------------------------------------------------------------------- le_avdata_SessionStateHandlerRef_t le_avdata_AddSessionStateHandler ( le_avdata_SessionStateHandlerFunc_t handlerPtr, void* contextPtr ) { LE_PRINT_VALUE("%p", handlerPtr); LE_PRINT_VALUE("%p", contextPtr); le_event_HandlerRef_t handlerRef = le_event_AddLayeredHandler("AVSessionState", SessionStateEvent, FirstLayerSessionStateHandler, (le_event_HandlerFunc_t)handlerPtr); le_event_SetContextPtr(handlerRef, contextPtr); return (le_avdata_SessionStateHandlerRef_t)(handlerRef); }
void test2(void) { HandlerRef = AddTestAHandler(HandleTestA, &SomeData); LE_PRINT_VALUE("%p", HandlerRef); // Try removing the handler and registering again, to ensure that allocated data objects // have been released, i.e. the associated client and server pools should not increase. RemoveTestAHandler(HandlerRef); HandlerRef = AddTestAHandler(HandleTestA, &SomeData); LE_PRINT_VALUE("%p", HandlerRef); LE_DEBUG("Triggering TestA\n"); TriggerTestA(); // Need to allow the event loop to process the trigger. // The rest of the test will be continued in the handler. }
//-------------------------------------------------------------------------------------------------- le_cellnet_StateEventHandlerRef_t le_cellnet_AddStateEventHandler ( le_cellnet_StateHandlerFunc_t handlerPtr, void* contextPtr ) { LE_PRINT_VALUE("%p", handlerPtr); LE_PRINT_VALUE("%p", contextPtr); le_event_HandlerRef_t handlerRef = le_event_AddLayeredHandler( "CellNetState", CellNetStateEvent, FirstLayerCellNetStateHandler, handlerPtr); le_event_SetContextPtr(handlerRef, contextPtr); return (le_cellnet_StateEventHandlerRef_t)(handlerRef); }
//-------------------------------------------------------------------------------------------------- static void DownloadFirmware ( const char* fileName ///< Name of file containing firmware image ) { int fd; if ( strcmp(fileName, "-") == 0 ) { // Use stdin fd = STDIN_FILENO; } else { // Open the file fd = open( fileName, O_RDONLY); LE_PRINT_VALUE("%d", fd); if ( fd == -1 ) { // Inform the user of the error; it's also useful to log this info printf("Can't open file '%s' : %m\n", fileName); LE_FATAL("Can't open file '%s' : %m", fileName); } } TryConnect(le_fwupdate_ConnectService, "fwupdateService"); // Connected to service so continue printf("Download started ...\n"); fflush(stdout); LE_PRINT_VALUE("%d", fd); if ( le_fwupdate_Download(fd) == LE_OK ) { printf("Download successful; please wait for modem to reset\n"); } else { printf("Error in download\n"); } }
void test2(void) { HandlerRef = AddTestAHandler(HandleTestA, (void*)ClientMessage); LE_PRINT_VALUE("%p", HandlerRef); LE_DEBUG("Triggering TestA\n"); TriggerTestA(); // Need to allow the event loop to process the trigger. // The rest of the test will be continued in the handler. }
//-------------------------------------------------------------------------------------------------- static void SendCellNetStateEvent ( void ) { le_mrc_NetRegState_t state; le_mrc_GetNetRegState(&state); le_cellnet_State_t cellNetState = TranslateToCellNetState(state); LE_PRINT_VALUE("%i", cellNetState); // Send the event to interested applications le_event_Report(CellNetStateEvent, &cellNetState, sizeof(cellNetState)); }
// ------------------------------------------------------------------------------------------------- static void MrcNetRegHandler ( le_mrc_NetRegState_t state, void* contextPtr ) { le_cellnet_State_t cellNetState = TranslateToCellNetState(state); LE_PRINT_VALUE("Cellular Network Registration state.%d", cellNetState); // Send the state event to applications le_event_Report(CellNetStateEvent, &cellNetState, sizeof(cellNetState)); }
void TriggerTestA ( void ) { // todo: This could instead be the value passed into the trigger function, but need // to change the .api definition for that to work. static int32_t triggerCount=0; triggerCount++; LE_PRINT_VALUE("%d", triggerCount); le_event_Report(TriggerEvent, &triggerCount, sizeof(triggerCount)); }
//-------------------------------------------------------------------------------------------------- static void Field_settingStringOne_Handler ( le_avdata_AssetInstanceRef_t instRef, const char* fieldName, void* contextPtr ) { char strBuf[LE_AVDATA_STRING_VALUE_LEN]; LE_INFO("Registered handler called for %s", fieldName); // Log the value written by the server le_avdata_GetString(instRef, "settingStringOne", strBuf, sizeof(strBuf)); LE_PRINT_VALUE("%s", strBuf); }
//-------------------------------------------------------------------------------------------------- le_avdata_AssetInstanceRef_t le_avdata_Create ( const char* assetName ///< [IN] ) { // Get the client's credentials. pid_t pid; uid_t uid; if (le_msg_GetClientUserCreds(le_avdata_GetClientSessionRef(), &uid, &pid) != LE_OK) { LE_KILL_CLIENT("Could not get credentials for the client."); return NULL; } // Look up the process's application name. char appName[LE_LIMIT_APP_NAME_LEN+1]; le_result_t result = le_appInfo_GetName(pid, appName, sizeof(appName)); LE_FATAL_IF(result == LE_OVERFLOW, "Buffer too small to contain the application name."); // TODO: Should this be LE_KILL_CLIENT instead? LE_FATAL_IF(result != LE_OK, "Could not get app name"); // Create an instance of the asset assetData_InstanceDataRef_t instRef; int instanceId; LE_ASSERT( assetData_CreateInstanceByName(appName, assetName, -1, &instRef) == LE_OK ); LE_ASSERT( instRef != NULL ); LE_ASSERT( assetData_GetInstanceId(instRef, &instanceId) == LE_OK ); LE_PRINT_VALUE("%i", instanceId); // Return a safe reference for the instance InstanceRefData_t* instRefDataPtr = le_mem_ForceAlloc(InstanceRefDataPoolRef); instRefDataPtr->clientSessionRef = le_avdata_GetClientSessionRef(); instRefDataPtr->instRef = instRef; instRef = le_ref_CreateRef(InstanceRefMap, instRefDataPtr); return instRef; }
void RemoveTestA ( TestARef_t addHandlerRef ) { LE_PRINT_VALUE("%p", addHandlerRef); if ( addHandlerRef == (TestARef_t)10 ) { HandlerRef = NULL; ContextPtr = NULL; } else { LE_ERROR("Invalid addHandlerRef='%p'\n", addHandlerRef); } }
//-------------------------------------------------------------------------------------------------- void le_cellnet_Release ( le_cellnet_RequestObjRef_t requestRef ///< Reference to a previously requested cellular network ) { // Look up the reference. If it is NULL, then the reference is not valid. // Otherwise, delete the reference and send the release command to the CellNet thread. void* cellNetPtr = le_ref_Lookup(RequestRefMap, requestRef); if ( cellNetPtr == NULL ) { LE_ERROR("Invalid cellular network request reference %p", requestRef); } else { LE_PRINT_VALUE("%p", requestRef); le_ref_DeleteRef(RequestRefMap, requestRef); uint32_t command = RELEASE_COMMAND; le_event_Report(CommandEvent, &command, sizeof(command)); } }
void test1(void) { uint32_t value=10; uint32_t data[] = {1, 2, 3, 4}; size_t length=10; uint32_t output[length]; char response[21]; char more[21]; allParameters(COMMON_TWO, &value, data, 4, output, &length, "input string", response, sizeof(response), more, sizeof(more)); LE_PRINT_VALUE("%i", value); LE_PRINT_ARRAY("%i", length, output); LE_PRINT_VALUE("%s", response); LE_PRINT_VALUE("%s", more); // Call again with a special value, so that nothing is returned for the 'output', 'response' // and 'more' output parameters. This could happen in a typical function, if an error is // detected. // Make 'length' larger than actually defined for the 'output' parameter to verify that // only the maximum defined value is used on the server. length = 20; allParameters(COMMON_ZERO, &value, data, 4, output, &length, "new string", response, sizeof(response), more, sizeof(more)); LE_PRINT_VALUE("%i", value); LE_PRINT_ARRAY("%i", length, output); LE_PRINT_VALUE("%s", response); LE_PRINT_VALUE("%s", more); // Test file descriptors int fdToServer; int fdFromServer; // Open a file known to exist fdToServer = open("/usr/include/stdio.h", O_RDONLY); LE_PRINT_VALUE("%i", fdToServer); FileTest(fdToServer, &fdFromServer); LE_PRINT_VALUE("%i", fdFromServer); // Read and print out whatever is read from the server fd writeFdToLog(fdFromServer); }