//-------------------------------------------------------------------------------------------------- static void AddImportFiles ( le_cfg_IteratorRef_t cfgIter, ///< [IN] Iterator to the application config. const ImportObj_t (*importsPtr)[], ///< [IN] Imports to include in the application. size_t numImports ///< [IN] Number of import elements. ) { // Find the last node under the 'files' or 'dirs' section. size_t nodeNum = 0; char nodePath[LIMIT_MAX_PATH_BYTES]; while (1) { int n = snprintf(nodePath, sizeof(nodePath), "%zd", nodeNum); INTERNAL_ERR_IF(n >= sizeof(nodePath), "Node name is too long."); INTERNAL_ERR_IF(n < 0, "Format error. %m"); if (!le_cfg_NodeExists(cfgIter, nodePath)) { break; } nodeNum++; } // Start adding files/directories at the end of the current list. int i; for (i = 0; i < numImports; i++) { // Add the source. int n = snprintf(nodePath, sizeof(nodePath), "%zd/src", nodeNum + i); INTERNAL_ERR_IF(n >= sizeof(nodePath), "Node name is too long."); INTERNAL_ERR_IF(n < 0, "Format error. %m"); le_cfg_SetString(cfgIter, nodePath, (*importsPtr)[i].src); // Add the destination. n = snprintf(nodePath, sizeof(nodePath), "%zd/dest", nodeNum + i); INTERNAL_ERR_IF(n >= sizeof(nodePath), "Node name is too long."); INTERNAL_ERR_IF(n < 0, "Format error. %m"); le_cfg_SetString(cfgIter, nodePath, (*importsPtr)[i].dest); } }
static void DeleteTest() { static char pathBuffer[STR_SIZE] = { 0 }; snprintf(pathBuffer, STR_SIZE, "%s/deleteTest/", TestRootDir); le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(pathBuffer); le_cfg_SetString(iterRef, "valueA", "aNewValue"); le_cfg_SetString(iterRef, "valueB", "aNewValue"); le_cfg_SetString(iterRef, "valueC", "aNewValue"); TestValue(iterRef, "valueA", "aNewValue"); TestValue(iterRef, "valueB", "aNewValue"); TestValue(iterRef, "valueC", "aNewValue"); le_cfg_CommitTxn(iterRef); iterRef = le_cfg_CreateWriteTxn(pathBuffer); le_cfg_DeleteNode(iterRef, "valueB"); TestValue(iterRef, "valueA", "aNewValue"); TestValue(iterRef, "valueB", ""); TestValue(iterRef, "valueC", "aNewValue"); le_cfg_CommitTxn(iterRef); iterRef = le_cfg_CreateReadTxn(pathBuffer); TestValue(iterRef, "valueA", "aNewValue"); TestValue(iterRef, "valueB", ""); TestValue(iterRef, "valueC", "aNewValue"); DumpTree(iterRef, 0); le_cfg_CancelTxn(iterRef); }
static void ExistAndEmptyTest() { static char pathBuffer[STR_SIZE] = { 0 }; snprintf(pathBuffer, STR_SIZE, "/%s/existAndEmptyTest/", TestRootDir); { le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(pathBuffer); le_cfg_SetEmpty(iterRef, ""); LE_TEST(le_cfg_IsEmpty(iterRef, "") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueA") == false); LE_TEST(le_cfg_NodeExists(iterRef, "valueB") == false); LE_TEST(le_cfg_NodeExists(iterRef, "valueC") == false); LE_TEST(le_cfg_NodeExists(iterRef, "valueD") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueA") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueB") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueC") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueD") == true); le_cfg_SetString(iterRef, "valueA", "aNewValue"); le_cfg_SetInt(iterRef, "valueB", 10); le_cfg_SetBool(iterRef, "valueC", true); le_cfg_SetFloat(iterRef, "valueD", 10.24); LE_TEST(le_cfg_NodeExists(iterRef, "valueA") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueB") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueC") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueD") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueA") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueB") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueC") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueD") == false); le_cfg_CommitTxn(iterRef); } }
//-------------------------------------------------------------------------------------------------- static void ConfigureGdb ( void ) { le_cfg_ConnectService(); le_cfgAdmin_ConnectService(); // Get a write iterator to the application node. le_cfg_IteratorRef_t cfgIter = le_cfg_CreateWriteTxn("/apps"); le_cfg_GoToNode(cfgIter, AppName); // Check if this is a temporary configuration that was previously created by this or a similar // tool. if (!le_cfg_IsEmpty(cfgIter, CFG_DEBUG_TOOL)) { char debugTool[LIMIT_MAX_PATH_BYTES]; // Don't need to check return code because the value is just informative and does not matter // if it is truncated. le_cfg_GetString(cfgIter, CFG_DEBUG_TOOL, debugTool, sizeof(debugTool), ""); fprintf(stderr, "This application has already been configured for %s debug mode.\n", debugTool); exit(EXIT_FAILURE); } // Write into the config's debug tool node to indicate that this configuration has been modified. le_cfg_SetString(cfgIter, CFG_DEBUG_TOOL, "gdb"); // Add 512K to the maxFileSytemBytes so that we can debug this app in sandboxed mode uint32_t maxBytes; maxBytes = le_cfg_GetInt(cfgIter, "maxFileSystemBytes", DEFAULT_LIMIT_MAX_FILE_SYSTEM_BYTES); maxBytes += ADD_FILE_SYSTEM_BYTES; // add an additional 512KBytes LE_INFO("Resetting maxFileSystemBytes to %d bytes", maxBytes); le_cfg_SetInt(cfgIter, "maxFileSystemBytes", maxBytes); // Add gdbserver and libs to the app's 'requires/files' section. le_cfg_GoToNode(cfgIter, "requires/files"); AddImportFiles(cfgIter, &GdbFilesImports, NUM_ARRAY_MEMBERS(GdbFilesImports)); // Add /proc to the app's dirs section. le_cfg_GoToParent(cfgIter); le_cfg_GoToNode(cfgIter, "dirs"); AddImportFiles(cfgIter, &GdbDirsImports, NUM_ARRAY_MEMBERS(GdbDirsImports)); // Delete the list of processes. le_cfg_GoToParent(cfgIter); le_cfg_GoToParent(cfgIter); int i; for (i = 0; i < NumProcs; i++) { char nodePath[LIMIT_MAX_PATH_BYTES]; int n = snprintf(nodePath, sizeof(nodePath), "procs/%s", ProcNames[i]); INTERNAL_ERR_IF(n >= sizeof(nodePath), "Node name is too long."); INTERNAL_ERR_IF(n < 0, "Format error. %m"); le_cfg_DeleteNode(cfgIter, nodePath); } le_cfg_CommitTxn(cfgIter); }
//-------------------------------------------------------------------------------------------------- le_result_t le_cellnet_SetSimPinCode ( le_sim_Id_t simId, ///< [IN] ///< SIM identifier. const char* pinCodePtr ///< [IN] ///< PIN code to insert in the config tree. ) { le_result_t result=LE_OK; size_t pinCodeLength = strlen(pinCodePtr); LE_DEBUG("simId= %d, pinCode= %s",simId,pinCodePtr); if (simId >= LE_SIM_ID_MAX) { LE_ERROR("Invalid simId (%d) provided!", simId); result = LE_OUT_OF_RANGE; } else { //void entry is taken into account if (strncmp(pinCodePtr,"",LE_SIM_PIN_MAX_LEN)!=0) { if (pinCodeLength > LE_SIM_PIN_MAX_LEN) { LE_KILL_CLIENT("PIN code exceeds %d", LE_SIM_PIN_MAX_LEN); return LE_FAULT; } else if (pinCodeLength < LE_SIM_PIN_MIN_LEN) { LE_ERROR("SIM PIN code is not long enough (min 4 digits)"); result = LE_UNDERFLOW; } else { // test SIM pincode format int i; bool test_ok = true; for (i=0; ((i<pinCodeLength) && test_ok); i++) { if ((pinCodePtr[i] < 0x30) || (pinCodePtr[i] > 0x39)) { test_ok = false; break; } } if (false == test_ok) { LE_ERROR("SIM PIN code format error"); result = LE_FORMAT_ERROR; } } } } if (LE_OK == result) { // Set the configuration path for the SIM. char configPath[LIMIT_MAX_PATH_BYTES]; snprintf(configPath, sizeof(configPath), "%s/%d", CFG_MODEMSERVICE_SIM_PATH, simId); le_cfg_IteratorRef_t simCfgRef = le_cfg_CreateWriteTxn(configPath); le_cfg_SetString(simCfgRef, CFG_NODE_PIN, pinCodePtr); le_cfg_CommitTxn(simCfgRef); LE_DEBUG("SIM PIN code (%s) inserted OK in config Tree",pinCodePtr); // New SIM pincode is taken into account LoadSimFromConfigDb(simId); SendCellNetStateEvent(); } return result; }
// ------------------------------------------------------------------------------------------------- static int HandleSet ( void ) // ------------------------------------------------------------------------------------------------- { // Looks like we're trying to write a value to a node. Get the node's current type and then // write the requested value to that node. le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(NodePath); le_cfg_nodeType_t originalType = le_cfg_GetNodeType(iterRef, ""); le_cfg_nodeType_t newType = DataType; if ( (newType != originalType) && (originalType != LE_CFG_TYPE_DOESNT_EXIST)) { printf("Converting node '%s' type from %s to %s.\n", NodePath, NodeTypeStr(originalType), NodeTypeStr(newType)); } int result = EXIT_SUCCESS; switch (newType) { case LE_CFG_TYPE_STRING: le_cfg_SetString(iterRef, "", NodeValue); break; case LE_CFG_TYPE_BOOL: if (strcmp(NodeValue, "false") == 0) { le_cfg_SetBool(iterRef, "", false); } else if (strcmp(NodeValue, "true") == 0) { le_cfg_SetBool(iterRef, "", true); } else { fprintf(stderr, "Bad boolean value '%s'.\n", NodeValue); } break; case LE_CFG_TYPE_INT: { char *endIntp; errno = 0; int32_t value = strtol(NodeValue, &endIntp, 10); if (errno != 0) { fprintf(stderr, "Integer '%s' out of range\n", NodeValue); result = EXIT_FAILURE; } else if (*endIntp != '\0') { fprintf(stderr, "Invalid character in integer '%s'\n", NodeValue); result = EXIT_FAILURE; } else { le_cfg_SetInt(iterRef, "", value); } break; } case LE_CFG_TYPE_FLOAT: { char *endFloatp; errno = 0; double floatVal = strtod(NodeValue, &endFloatp); if (errno != 0) { fprintf(stderr, "Float value '%s' out of range\n", NodeValue); result = EXIT_FAILURE; } else if (*endFloatp != '\0') { fprintf(stderr, "Invalid character in float value '%s'\n", NodeValue); result = EXIT_FAILURE; } else { le_cfg_SetFloat(iterRef, "", floatVal); } break; } case LE_CFG_TYPE_DOESNT_EXIST: result = EXIT_FAILURE; break; default: fprintf(stderr, "Unexpected node type specified, %s.\n", NodeTypeStr(newType)); result = EXIT_FAILURE; break; } // Finally, commit the value update, if the set was successful. if (result != EXIT_FAILURE) { le_cfg_CommitTxn(iterRef); } else { le_cfg_CancelTxn(iterRef); } return result; }
// ------------------------------------------------------------------------------------------------- static le_result_t HandleImportJSONIteration ( le_cfg_IteratorRef_t iterRef, ///< Dump the JSON data into this iterator. json_t* nodePtr ///< From this JSON object. ) // ------------------------------------------------------------------------------------------------- { // Get value json_t* value = json_object_get(nodePtr, JSON_FIELD_VALUE); // Check type const char* typeStr = json_string_value(json_object_get(nodePtr, JSON_FIELD_TYPE)); le_cfg_nodeType_t type = GetNodeTypeFromString(typeStr); switch (type) { case LE_CFG_TYPE_BOOL: le_cfg_SetBool(iterRef, "", json_is_true(value)); break; case LE_CFG_TYPE_STRING: le_cfg_SetString(iterRef, "", json_string_value(value)); break; case LE_CFG_TYPE_INT: le_cfg_SetInt(iterRef, "", json_integer_value(value)); break; case LE_CFG_TYPE_FLOAT: le_cfg_SetFloat(iterRef, "", json_real_value(value)); break; case LE_CFG_TYPE_STEM: { // Iterate on children json_t* childrenPtr = json_object_get(nodePtr, JSON_FIELD_CHILDREN); json_t* childPtr; int i; json_array_foreach(childrenPtr, i, childPtr) { // Get name const char* name = json_string_value(json_object_get(childPtr, JSON_FIELD_NAME)); // Is node exist with this name? le_cfg_nodeType_t existingType = le_cfg_GetNodeType(iterRef, name); switch (existingType) { case LE_CFG_TYPE_DOESNT_EXIST: case LE_CFG_TYPE_STEM: case LE_CFG_TYPE_EMPTY: // Not existing, already a stem or empty node, nothing to do break; default: // Issue with node creation fprintf(stderr, "Node conflict when importing, at node %s", name); return LE_NOT_POSSIBLE; break; } // Iterate to this child le_cfg_GoToNode(iterRef, name); // Iterate le_result_t subResult = HandleImportJSONIteration(iterRef, childPtr); if (subResult != LE_OK) { // Something went wrong return subResult; } // Go back to parent le_cfg_GoToParent(iterRef); } } break; default: return LE_FAULT; } return LE_OK; }