char *gp_config_get_secname(struct gp_ini_context *ctx, int i) { struct ini_cfgobj *ini_config = (struct ini_cfgobj *)ctx->private_data; char **list = NULL; int count; int error; char *secname; list = ini_get_section_list(ini_config, &count, &error); if (error) { return NULL; } if (i >= count) { return NULL; } secname = strdup(list[i]); ini_free_section_list(list); if (!secname) { return NULL; } return secname; }
int gp_config_get_nsec(struct gp_ini_context *ctx) { struct ini_cfgobj *ini_config = (struct ini_cfgobj *)ctx->private_data; char **list = NULL; int count; int error; list = ini_get_section_list(ini_config, &count, &error); if (error) { return 0; } ini_free_section_list(list); return count; }
int main(int argc, char **argv) { int i; char *list[32]; char buf[50]; i = ini_get_section_list(argv[1], &list[0], 32); for (i = i - 1; i >= 0; i--) { printf("%s\n", list[i]); printf("\tType = %s\n", ini_get_key_value(list[i], "InterfaceType", argv[1], buf, sizeof(buf))); printf("\tIP = %s\n", ini_get_key_value(list[i], "SlotNr", argv[1], buf, sizeof(buf))); free(list[i]); } return 0; }
int main (int argc, char **argv) { struct ini_cfgobj *cfg_ctx = NULL; struct ini_cfgfile *file_ctx = NULL; char **section_list = NULL; int section_count = 0; char **attribute_list = NULL; int attribute_count = 0; struct value_obj *vo = NULL; int result = 0; char *value = NULL; char *path = "/usr/lib/systemd/system/docker.service"; printf("%s\n", path); // This throws an error as incomplete because the struct def is within the .so //file_ctx = malloc(sizeof(struct ini_cfgfile)); if ((result = ini_config_create(&cfg_ctx)) != 0) { printf("Error occured %d\n", result); goto cleanup; } //I suspect that file_ctx may be a member of struct cfg_ctx if ((result = ini_config_file_open(path, 0, &file_ctx)) != 0) { printf("Error occured %d\n", result); goto cleanup; } // This sets INI_MV2S_OVERWRITE by default, so when we parse the /etc version, it overwrites the matching section->attr // Should test this .... if ((result = ini_config_parse(file_ctx, 0, INI_MV1S_ALLOW, 0, cfg_ctx)) != 0) { printf("Error occured during config parsing %d\n", result); } result = 0; section_list = ini_get_section_list(cfg_ctx, §ion_count, &result); if (result != 0) { printf("Error while parsing section list %d\n", result); } for (int i = 0; i < section_count; ++i) { printf("Section\n%s\n ------\n", section_list[i]); attribute_list = NULL; result = 0; attribute_list = ini_get_attribute_list(cfg_ctx, section_list[i], &attribute_count, &result); if (result != 0) { printf("Error parsing attribute list\n"); goto next; } for (int j = 0; j < attribute_count; ++j) { // So we end up with MANY elements of the same attr name in attribute list ... How do we handle this? printf("attr: %s -> ", attribute_list[j]); if ((result = ini_get_config_valueobj(section_list[i], attribute_list[j], cfg_ctx, INI_GET_NEXT_VALUE, &vo)) != 0) { printf("\n Error retriving attribute value"); goto next; } if (vo == NULL) { goto nextattr; } result = 0; value = ini_get_string_config_value(vo, &result); if (result != 0) { printf("\n Error parsing attribute value"); goto next; } printf("%s ; ", value); nextattr: printf("\n"); } next: if (value != NULL) { free(value); value = NULL; } if (vo != NULL) { //How do we free this? //ini_ vo = NULL; } if (attribute_list != NULL) { ini_free_attribute_list(attribute_list); attribute_list = NULL; } } cleanup: if (section_list != NULL) { ini_free_section_list(section_list); } if (file_ctx != NULL) { // Is there a memory leak here .... wooooeeeeeoooooooo ini_config_file_close(file_ctx); } if (cfg_ctx != NULL) { ini_config_destroy(cfg_ctx); } return result; }
/** * Open given CAN channel * @param ucChannel channel name or device fiel */ int CPC_OpenChannel(char *ucChannel) { static unsigned int firstCall = 1; CPC_MSG_T msg; unsigned int slot, i; int retval, fd; #ifdef _CPC_CONF_INI char *sectionList[32]; char valueBuffer[50]; int sectionCount; int minor = 0; int tmp = 0; int found = 0; unsigned char configIrq = 0; unsigned int irqNr; #endif int intf = 0; char *devFile = NULL; // Initialize internal structures on first call if (firstCall) { for (i = 0; i < CAN_MAX_DEVICE; i++) { CPC_SJA1000_PARAMS_T *p = &CPCInitParams[i].canparams.cc_params.sja1000; CPCHandlerCnt[i] = 0; CPCInitParams[i].chanparams.fd = -1; CPCInitParams[i].canparams.cc_type = SJA1000; p->acc_code0 = p->acc_code1 = p->acc_code2 = p->acc_code3 = 0x55; p->acc_mask0 = p->acc_mask1 = p->acc_mask2 = p->acc_mask3 = 0xFF; p->mode = p->btr0 = p->btr1 = p->outp_contr = 0x00; CPCHandlerCnt[i] = 0; } firstCall = 0; } // Find free slot for (slot = 0; slot < CAN_MAX_DEVICE; slot++) { if (CPCInitParams[slot].chanparams.fd == -1) break; } if (slot >= CAN_MAX_DEVICE) return CPC_ERR_NO_FREE_CHANNEL; #ifdef _CPC_CONF_INI // Get section list from cpcconf.ini tmp = sectionCount = ini_get_section_list(CPC_CONF_INI_PATH, §ionList[0], 32); // cpcconf.ini not found if (sectionCount <= 0) { if (BE_INFORMATIVE) printf("CPCLIB[%s]: File '%s' not found or empty\n", __PRETTY_FUNCTION__, CPC_CONF_INI_PATH); return CPC_ERR_NO_INIFILE_PRESENT; } retval = 0; // Iterate over every section and try to find ucChannel for (--sectionCount; sectionCount >= 0; sectionCount--) { if (strcmp(ucChannel, sectionList[sectionCount]) == 0) { // ucChannel found now search InterfaceType to determine the right settings */ if (!ini_get_key_value(sectionList[sectionCount], "InterfaceType", CPC_CONF_INI_PATH, valueBuffer, sizeof(valueBuffer))) { if (BE_INFORMATIVE) printf("CPCLIB[%s]: 'InterfaceType' was not found in" "'%s::%s'\n", __PRETTY_FUNCTION__, CPC_CONF_INI_PATH, sectionList[sectionCount]); retval = CPC_ERR_WRONG_PARAMETERS; break; } if (BE_DEBUG) printf("CPCLIB[%s]: Channel '%s' and interface '%s' found\n", __PRETTY_FUNCTION__, ucChannel, valueBuffer); found = 1; intf = _CPC_InterfaceStrToNumber(valueBuffer); // Check if interface type is known switch (intf) { case CPCCARD: /* CPC-ECO Card */ minor = _CPC_DetermineDeviceFile(ucChannel, intf); if (minor < 0) { if (BE_INFORMATIVE) printf("CPCLIB[%s]: ERROR _CPC_DetermineDeviceFile()" " returned %d\n", __PRETTY_FUNCTION__, minor); retval = CPC_ERR_NO_MATCHING_CHANNEL; break; } sprintf(valueBuffer, "/dev/cpc_card%d", minor); devFile = valueBuffer; break; case CPCXT200: case CPCXT1000: /* CPC-XT Card */ minor = _CPC_DetermineDeviceFile(ucChannel, intf); if (minor < 0) { if (BE_INFORMATIVE) printf("CPCLIB[%s]: ERROR _CPC_DetermineDeviceFile()" " returned %d\n", __PRETTY_FUNCTION__, minor); retval = CPC_ERR_NO_MATCHING_CHANNEL; break; } sprintf(valueBuffer, "/dev/cpc_xt%d", minor); devFile = valueBuffer; configIrq = 1; break; case CPCPCI: /* CPC-PCI Card */ minor = _CPC_DetermineDeviceFile(ucChannel, intf); if (minor < 0) { if (BE_INFORMATIVE) printf("CPCLIB[%s]: ERROR _CPC_DetermineDeviceFile()" " returned %d\n", __PRETTY_FUNCTION__, minor); retval = CPC_ERR_NO_MATCHING_CHANNEL; break; } sprintf(valueBuffer, "/dev/cpc_pci%d", minor); devFile = valueBuffer; break; case CPCUSB: /* CPC-USB device */ minor = _CPC_DetermineDeviceFile(ucChannel, intf); if (minor < 0) { if (BE_INFORMATIVE) printf("CPCLIB[%s]: ERROR _CPC_DetermineDeviceFile()" " returned %d\n", __PRETTY_FUNCTION__, minor); retval = CPC_ERR_NO_MATCHING_CHANNEL; break; } sprintf(valueBuffer, "/dev/usb/cpc_usb%d", minor); devFile = valueBuffer; break; case CPCECO: /* CPC-ECO device */ minor = _CPC_DetermineDeviceFile(ucChannel, intf); if (minor < 0) { if (BE_INFORMATIVE) printf("CPCLIB[%s]: ERROR _CPC_DetermineDeviceFile()" " returned %d\n", __PRETTY_FUNCTION__, minor); retval = CPC_ERR_NO_MATCHING_CHANNEL; break; } sprintf(valueBuffer, "/dev/cpc_eco%d", minor); devFile = valueBuffer; break; case ETHERCAN: /* Remote EtherCAN Interface */ { char ip[32], port[10]; if (ini_get_key_value(sectionList[sectionCount], "IpAddress", CPC_CONF_INI_PATH, ip, sizeof(ip))) { sprintf(valueBuffer, "%s:", ip); if (ini_get_key_value(sectionList[sectionCount], "IpPort", CPC_CONF_INI_PATH, port, sizeof(port))) { strcat(valueBuffer, port); devFile = valueBuffer; break; } } retval = CPC_ERR_WRONG_PARAMETERS; } break; #ifdef SOCKETCAN_SUPPORT case SOCKETCAN: { char iface[16]; if (ini_get_key_value(sectionList[sectionCount], "NetworkInterface", CPC_CONF_INI_PATH, iface, sizeof(iface))) { strcpy(valueBuffer, iface); devFile = valueBuffer; break; } retval = CPC_ERR_WRONG_PARAMETERS; } break; #endif case NONE: default: if (BE_INFORMATIVE) printf("CPCLIB[%s]: Unsupported interface '%s'\n", __PRETTY_FUNCTION__, valueBuffer); retval = CPC_ERR_NO_MATCHING_INTERFACE; break; } break; } } // For backward compatibility we provide the old device-file style access #ifndef _CPC_LIB_COMPAT_MODE if (!found) retval = CPC_ERR_NO_MATCHING_INTERFACE; #else // If the given entry was not found we assume it is a device file or ip address if (!found) devFile = ucChannel; #endif if (retval != 0) { for (--tmp; tmp >= 0; tmp--) free(sectionList[tmp]); return retval; } #else devFile = ucChannel; #endif if(intf == ETHERCAN) { if ((fd = setupEtherCANConnection(slot, devFile)) < 0) { if (BE_INFORMATIVE) perror(devFile); return CPC_ERR_CHANNEL_NOT_ACTIVE; } #ifdef SOCKETCAN_SUPPORT } else if(intf == SOCKETCAN) { if ((fd = socketcan_SetupConnection(slot, devFile)) < 0) { if (BE_INFORMATIVE) perror(devFile); return CPC_ERR_CHANNEL_NOT_ACTIVE; } #endif } else { if ((fd = open(devFile, O_RDWR)) >= 0) { CPCLibParams[slot].read = read; CPCLibParams[slot].write = write; } else { return CPC_ERR_CHANNEL_NOT_ACTIVE; } } if (BE_DEBUG) printf("CPCLIB[%s]: Device %s (%s) opened with handle: %d, fd: %d!\n", __PRETTY_FUNCTION__, devFile, ucChannel, slot, fd); CPCInitParams[slot].chanparams.fd = fd; // Reset device CPC_CANExit(slot, DO_NOT_CONFIRM); CPC_ClearCMDQueue(slot, DO_NOT_CONFIRM); CPC_ClearMSGQueue(slot); #ifndef __uClinux__ #ifdef _CPC_CONF_INI if (configIrq) { if (!ini_get_key_value(ucChannel, "IrqNr", CPC_CONF_INI_PATH, valueBuffer, sizeof(valueBuffer))) { if (BE_INFORMATIVE) printf("CPCLIB[%s]: Could find IRQ number\n", __PRETTY_FUNCTION__); CPC_CloseChannel(slot); return CPC_ERR_WRONG_PARAMETERS; } irqNr = atoi(valueBuffer); msg.type = CPC_CMD_T_OPEN_CHAN; msg.length = 2; msg.msg.generic[0] = (unsigned char) irqNr; retval = CPCLibParams[slot].write(CPCInitParams[slot].chanparams.fd, (void *) &msg, sizeof(CPC_MSG_T)); if (retval < 0) { if (BE_INFORMATIVE) printf("CPCLIB[%s]: Could not configure IRQ line\n", __PRETTY_FUNCTION__); CPC_CloseChannel(slot); return CPC_ERR_NO_RESOURCES; } } #endif #endif return slot; }