void common_process_controls(int fd) { find_controls(fd); for (ctrl_get_list::iterator iter = get_ctrls.begin(); iter != get_ctrls.end(); ++iter) { if (ctrl_str2q.find(*iter) == ctrl_str2q.end()) { fprintf(stderr, "unknown control '%s'\n", iter->c_str()); exit(1); } } for (ctrl_set_map::iterator iter = set_ctrls.begin(); iter != set_ctrls.end(); ++iter) { if (ctrl_str2q.find(iter->first) == ctrl_str2q.end()) { fprintf(stderr, "unknown control '%s'\n", iter->first.c_str()); exit(1); } } }
void common_process_controls(int fd) { struct v4l2_query_ext_ctrl qc = { V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND }; int rc; rc = test_ioctl(fd, VIDIOC_QUERY_EXT_CTRL, &qc); have_query_ext_ctrl = rc == 0; find_controls(fd); for (ctrl_get_list::iterator iter = get_ctrls.begin(); iter != get_ctrls.end(); ++iter) { if (ctrl_str2q.find(*iter) == ctrl_str2q.end()) { fprintf(stderr, "unknown control '%s'\n", iter->c_str()); exit(1); } } for (ctrl_set_map::iterator iter = set_ctrls.begin(); iter != set_ctrls.end(); ++iter) { if (ctrl_str2q.find(iter->first) == ctrl_str2q.end()) { fprintf(stderr, "unknown control '%s'\n", iter->first.c_str()); exit(1); } } }
static int snmp_rsa_discover_resources(void *hnd) { SaHpiEntityPathT entity_root; guint i; struct oh_event *e; struct snmp_value get_value; // struct snmp_value get_active; struct oh_handler_state *handle = (struct oh_handler_state *)hnd; struct snmp_rsa_hnd *custom_handle = (struct snmp_rsa_hnd *)handle->data; RPTable *tmpcache = (RPTable *)g_malloc0(sizeof(RPTable)); GSList *tmpqueue = NULL; char *root_tuple = (char *)g_hash_table_lookup(handle->config,"entity_root"); string2entitypath(root_tuple, &entity_root); /* see if the chassis exists by querying system health */ if(snmp_get(custom_handle->ss,".1.3.6.1.4.1.2.3.51.1.2.7.1.0",&get_value) != 0) { /* If we get here, something is hosed. No need to do more discovery */ dbg("Couldn't fetch SNMP RSA system health.\n"); dbg("There is no chassis."); g_free(tmpcache); return -1; } /* discover the chassis */ e = snmp_rsa_discover_chassis(handle, &entity_root); if(e != NULL) { struct ResourceMibInfo *res_mib = g_memdup(&(snmp_rsa_rpt_array[RSA_RPT_ENTRY_CHASSIS].rsa_res_info.mib), sizeof(struct snmp_rpt)); oh_add_resource(tmpcache,&(e->u.res_event.entry),res_mib,0); tmpqueue = g_slist_append(tmpqueue, e); SaHpiResourceIdT rid = e->u.res_event.entry.ResourceId; SaHpiEntityPathT parent_ep = e->u.res_event.entry.ResourceEntity; find_sensors(snmp_rsa_chassis_sensors); find_controls(snmp_rsa_chassis_controls); find_inventories(snmp_rsa_chassis_inventories); } /* discover all cpus */ for (i = 0; i < RSA_MAX_CPU; i++) { /* see if the cpu exists by querying the thermal sensor */ if((snmp_get(custom_handle->ss, snmp_rsa_cpu_thermal_sensors[i].rsa_sensor_info.mib.oid, &get_value) != 0) || (get_value.type != ASN_OCTET_STR) || (strcmp(get_value.string, "Not Readable!") == 0)) { /* If we get here the CPU is not installed */ dbg("CPU %d not found.\n", i+RSA_HPI_INSTANCE_BASE); continue; } e = snmp_rsa_discover_cpu(handle, &entity_root, i); if(e != NULL) { struct ResourceMibInfo *res_mib = g_memdup(&(snmp_rsa_rpt_array[RSA_RPT_ENTRY_CPU].rsa_res_info.mib), sizeof(struct snmp_rpt)); oh_add_resource(tmpcache,&(e->u.res_event.entry),res_mib,0); tmpqueue = g_slist_append(tmpqueue, e); SaHpiResourceIdT rid = e->u.res_event.entry.ResourceId; SaHpiEntityPathT parent_ep = e->u.res_event.entry.ResourceEntity; /* add the CPU thermal sensor */ e = snmp_rsa_discover_sensors(handle, parent_ep, &snmp_rsa_cpu_thermal_sensors[i]); if(e != NULL) { struct RSA_SensorInfo *rsa_data = g_memdup(&(snmp_rsa_cpu_thermal_sensors[i].rsa_sensor_info), sizeof(struct RSA_SensorInfo)); oh_add_rdr(tmpcache,rid,&(e->u.rdr_event.rdr), rsa_data, 0); tmpqueue = g_slist_append(tmpqueue, e); } } } /* discover all dasd */ for (i = 0; i < RSA_MAX_DASD; i++) { /* see if the dasd exists by querying the thermal sensor */ if((snmp_get(custom_handle->ss, snmp_rsa_dasd_thermal_sensors[i].rsa_sensor_info.mib.oid, &get_value) != 0) || (get_value.type != ASN_OCTET_STR) || (strcmp(get_value.string, "Not Readable!") == 0)) { /* If we get here the DASD is not installed */ dbg("DASD %d not found.\n", i+RSA_HPI_INSTANCE_BASE); continue; } e = snmp_rsa_discover_dasd(handle, &entity_root, i); if(e != NULL) { struct ResourceMibInfo *res_mib = g_memdup(&(snmp_rsa_rpt_array[RSA_RPT_ENTRY_DASD].rsa_res_info.mib), sizeof(struct snmp_rpt)); oh_add_resource(tmpcache,&(e->u.res_event.entry),res_mib,0); tmpqueue = g_slist_append(tmpqueue, e); SaHpiResourceIdT rid = e->u.res_event.entry.ResourceId; SaHpiEntityPathT parent_ep = e->u.res_event.entry.ResourceEntity; /* add the DASD thermal sensor */ e = snmp_rsa_discover_sensors(handle, parent_ep, &snmp_rsa_dasd_thermal_sensors[i]); if(e != NULL) { struct RSA_SensorInfo *rsa_data = g_memdup(&(snmp_rsa_dasd_thermal_sensors[i].rsa_sensor_info), sizeof(struct RSA_SensorInfo)); oh_add_rdr(tmpcache,rid,&(e->u.rdr_event.rdr), rsa_data, 0); tmpqueue = g_slist_append(tmpqueue, e); } } } /* discover all fans */ for (i = 0; i < RSA_MAX_FAN; i++) { /* see if the fan exists by querying the sensor */ if((snmp_get(custom_handle->ss, snmp_rsa_fan_sensors[i].rsa_sensor_info.mib.oid, &get_value) != 0) || (get_value.type != ASN_OCTET_STR) || (strcmp(get_value.string, "Not Readable!") == 0)) { /* If we get here the fan is not installed */ dbg("Fan %d not found.\n", i+RSA_HPI_INSTANCE_BASE); continue; } e = snmp_rsa_discover_fan(handle, &entity_root, i); if(e != NULL) { struct ResourceMibInfo *res_mib = g_memdup(&(snmp_rsa_rpt_array[RSA_RPT_ENTRY_FAN].rsa_res_info.mib), sizeof(struct snmp_rpt)); oh_add_resource(tmpcache,&(e->u.res_event.entry),res_mib,0); tmpqueue = g_slist_append(tmpqueue, e); SaHpiResourceIdT rid = e->u.res_event.entry.ResourceId; SaHpiEntityPathT parent_ep = e->u.res_event.entry.ResourceEntity; /* add the fan sensor */ e = snmp_rsa_discover_sensors(handle, parent_ep, &snmp_rsa_fan_sensors[i]); if(e != NULL) { struct RSA_SensorInfo *rsa_data = g_memdup(&(snmp_rsa_fan_sensors[i].rsa_sensor_info), sizeof(struct RSA_SensorInfo)); oh_add_rdr(tmpcache,rid,&(e->u.rdr_event.rdr), rsa_data, 0); tmpqueue = g_slist_append(tmpqueue, e); } } } /* Rediscovery: Get difference between current rptcache and tmpcache. Delete obsolete items from rptcache and add new items in. */ GSList *res_new = NULL, *rdr_new = NULL, *res_gone = NULL, *rdr_gone = NULL; GSList *node = NULL; rpt_diff(handle->rptcache, tmpcache, &res_new, &rdr_new, &res_gone, &rdr_gone); dbg("%d resources have gone away.", g_slist_length(res_gone)); dbg("%d resources are new or have changed", g_slist_length(res_new)); for (node = rdr_gone; node != NULL; node = node->next) { SaHpiRdrT *rdr = (SaHpiRdrT *)node->data; SaHpiRptEntryT *res = oh_get_resource_by_ep(handle->rptcache, &(rdr->Entity)); /* Create remove rdr event and add to event queue */ struct oh_event *e = (struct oh_event *)g_malloc0(sizeof(struct oh_event)); if (e) { e->type = OH_ET_RDR_DEL; e->u.rdr_del_event.record_id = rdr->RecordId; e->u.rdr_del_event.parent_entity = rdr->Entity; handle->eventq = g_slist_append(handle->eventq, e); } else dbg("Could not allocate more memory to create event."); /* Remove rdr from plugin's rpt cache */ if (rdr && res) oh_remove_rdr(handle->rptcache, res->ResourceId, rdr->RecordId); else dbg("No valid resource or rdr at hand. Could not remove rdr."); } g_slist_free(rdr_gone); for (node = res_gone; node != NULL; node = node->next) { SaHpiRptEntryT *res = (SaHpiRptEntryT *)node->data; /* Create remove resource event and add to event queue */ struct oh_event *e = (struct oh_event *)g_malloc0(sizeof(struct oh_event)); if (e) { e->type = OH_ET_RESOURCE_DEL; e->u.res_del_event.resource_id = res->ResourceId; handle->eventq = g_slist_append(handle->eventq, e); } else dbg("Could not allocate more memory to create event."); /* Remove resource from plugin's rpt cache */ if (res) oh_remove_resource(handle->rptcache, res->ResourceId); else dbg("No valid resource at hand. Could not remove resource."); } g_slist_free(res_gone); for (node = res_new; node != NULL; node = node->next) { GSList *tmpnode = NULL; SaHpiRptEntryT *res = (SaHpiRptEntryT *)node->data; if (!res) { dbg("No valid resource at hand. Could not process new resource."); continue; } gpointer data = oh_get_resource_data(tmpcache, res->ResourceId); oh_add_resource(handle->rptcache, res, g_memdup(data, sizeof(struct snmp_rpt)),0); /* Add new/changed resources to the event queue */ for (tmpnode = tmpqueue; tmpnode != NULL; tmpnode = tmpnode->next) { struct oh_event *e = (struct oh_event *)tmpnode->data; if (e->type == OH_ET_RESOURCE && e->u.res_event.entry.ResourceId == res->ResourceId) { handle->eventq = g_slist_append(handle->eventq, e); tmpqueue = g_slist_remove_link(tmpqueue, tmpnode); g_slist_free_1(tmpnode); break; } } } g_slist_free(res_new); for (node = rdr_new; node != NULL; node = node->next) { guint rdr_data_size = 0; GSList *tmpnode = NULL; SaHpiRdrT *rdr = (SaHpiRdrT *)node->data; SaHpiRptEntryT *res = oh_get_resource_by_ep(handle->rptcache, &(rdr->Entity)); if (!res || !rdr) { dbg("No valid resource or rdr at hand. Could not process new rdr."); continue; } gpointer data = oh_get_rdr_data(tmpcache, res->ResourceId, rdr->RecordId); /* Need to figure out the size of the data associated with the rdr */ if (rdr->RdrType == SAHPI_SENSOR_RDR) rdr_data_size = sizeof(struct RSA_SensorMibInfo); else if (rdr->RdrType == SAHPI_CTRL_RDR) rdr_data_size = sizeof(struct RSA_ControlMibInfo); else if (rdr->RdrType == SAHPI_INVENTORY_RDR) rdr_data_size = sizeof(struct RSA_InventoryMibInfo); oh_add_rdr(handle->rptcache, res->ResourceId, rdr, g_memdup(data, rdr_data_size),0); /* Add new/changed rdrs to the event queue */ for (tmpnode = tmpqueue; tmpnode != NULL; tmpnode = tmpnode->next) { struct oh_event *e = (struct oh_event *)tmpnode->data; if (e->type == OH_ET_RDR && ep_cmp(&(e->u.rdr_event.rdr.Entity),&(rdr->Entity)) == 0 && e->u.rdr_event.rdr.RecordId == rdr->RecordId) { handle->eventq = g_slist_append(handle->eventq, e); tmpqueue = g_slist_remove_link(tmpqueue, tmpnode); g_slist_free_1(tmpnode); break; } } } g_slist_free(rdr_new); /* Clean up tmpqueue and tmpcache */ g_slist_free(tmpqueue); oh_flush_rpt(tmpcache); g_free(tmpcache); return SA_OK; }
int main(int argc, char **argv) { char *value, *subs; int i; int fd = -1; /* command args */ int ch; const char *device = "/dev/video0"; /* -d device */ struct v4l2_capability vcap; /* list_cap */ char short_options[26 * 2 * 2 + 1]; int idx = 0; if (argc == 1) { usage(); return 0; } for (i = 0; long_options[i].name; i++) { if (!isalpha(long_options[i].val)) continue; short_options[idx++] = long_options[i].val; if (long_options[i].has_arg == required_argument) short_options[idx++] = ':'; } while (1) { int option_index = 0; short_options[idx] = 0; ch = getopt_long(argc, argv, short_options, long_options, &option_index); if (ch == -1) break; options[(int)ch] = 1; switch (ch) { case OptHelp: usage(); return 0; case OptSetDevice: device = optarg; if (device[0] >= '0' && device[0] <= '9' && device[1] == 0) { static char newdev[20]; char dev = device[0]; sprintf(newdev, "/dev/video%c", dev); device = newdev; } break; case OptGetCtrl: subs = optarg; while (*subs != '\0') { parse_next_subopt(&subs, &value); if (strchr(value, '=')) { usage(); exit(1); } else { get_ctrls.push_back(value); } } break; case OptSetCtrl: subs = optarg; while (*subs != '\0') { parse_next_subopt(&subs, &value); if (const char *equal = strchr(value, '=')) { set_ctrls[std::string(value, (equal - value))] = equal + 1; } else { fprintf(stderr, "control '%s' without '='\n", value); exit(1); } } break; case ':': fprintf(stderr, "Option `%s' requires a value\n", argv[optind]); usage(); return 1; case '?': fprintf(stderr, "Unknown argument `%s'\n", argv[optind]); usage(); return 1; } } if (optind < argc) { printf("unknown arguments: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); usage(); return 1; } if ((fd = open(device, O_RDWR)) < 0) { fprintf(stderr, "Failed to open %s: %s\n", device, strerror(errno)); exit(1); } doioctl(fd, VIDIOC_QUERYCAP, &vcap, "VIDIOC_QUERYCAP"); capabilities = vcap.capabilities; find_controls(fd); for (ctrl_get_list::iterator iter = get_ctrls.begin(); iter != get_ctrls.end(); ++iter) { if (ctrl_str2id.find(*iter) == ctrl_str2id.end()) { fprintf(stderr, "unknown control '%s'\n", (*iter).c_str()); exit(1); } } for (ctrl_set_map::iterator iter = set_ctrls.begin(); iter != set_ctrls.end(); ++iter) { if (ctrl_str2id.find(iter->first) == ctrl_str2id.end()) { fprintf(stderr, "unknown control '%s'\n", iter->first.c_str()); exit(1); } } /* Information Opts */ if (options[OptSetCtrl] && !set_ctrls.empty()) { struct v4l2_ext_controls ctrls = { 0 }; for (ctrl_set_map::iterator iter = set_ctrls.begin(); iter != set_ctrls.end(); ++iter) { struct v4l2_ext_control ctrl = { 0 }; ctrl.id = ctrl_str2id[iter->first]; ctrl.value = strtol(iter->second.c_str(), NULL, 0); if (V4L2_CTRL_ID2CLASS(ctrl.id) == V4L2_CTRL_CLASS_MPEG) mpeg_ctrls.push_back(ctrl); else user_ctrls.push_back(ctrl); } for (unsigned i = 0; i < user_ctrls.size(); i++) { struct v4l2_control ctrl; ctrl.id = user_ctrls[i].id; ctrl.value = user_ctrls[i].value; if (doioctl(fd, VIDIOC_S_CTRL, &ctrl, "VIDIOC_S_CTRL")) { fprintf(stderr, "%s: %s\n", ctrl_id2str[ctrl.id].c_str(), strerror(errno)); } } if (mpeg_ctrls.size()) { ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG; ctrls.count = mpeg_ctrls.size(); ctrls.controls = &mpeg_ctrls[0]; if (doioctl(fd, VIDIOC_S_EXT_CTRLS, &ctrls, "VIDIOC_S_EXT_CTRLS")) { if (ctrls.error_idx >= ctrls.count) { fprintf(stderr, "Error setting MPEG controls: %s\n", strerror(errno)); } else { fprintf(stderr, "%s: %s\n", ctrl_id2str[mpeg_ctrls[ctrls.error_idx].id].c_str(), strerror(errno)); } } } } /* Get options */ if (options[OptGetCtrl] && !get_ctrls.empty()) { struct v4l2_ext_controls ctrls = { 0 }; mpeg_ctrls.clear(); user_ctrls.clear(); for (ctrl_get_list::iterator iter = get_ctrls.begin(); iter != get_ctrls.end(); ++iter) { struct v4l2_ext_control ctrl = { 0 }; ctrl.id = ctrl_str2id[*iter]; if (V4L2_CTRL_ID2CLASS(ctrl.id) == V4L2_CTRL_CLASS_MPEG) mpeg_ctrls.push_back(ctrl); else user_ctrls.push_back(ctrl); } for (unsigned i = 0; i < user_ctrls.size(); i++) { struct v4l2_control ctrl; ctrl.id = user_ctrls[i].id; doioctl(fd, VIDIOC_G_CTRL, &ctrl, "VIDIOC_G_CTRL"); printf("%s: %d\n", ctrl_id2str[ctrl.id].c_str(), ctrl.value); } if (mpeg_ctrls.size()) { ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG; ctrls.count = mpeg_ctrls.size(); ctrls.controls = &mpeg_ctrls[0]; doioctl(fd, VIDIOC_G_EXT_CTRLS, &ctrls, "VIDIOC_G_EXT_CTRLS"); for (unsigned i = 0; i < mpeg_ctrls.size(); i++) { struct v4l2_ext_control ctrl = mpeg_ctrls[i]; printf("%s: %d\n", ctrl_id2str[ctrl.id].c_str(), ctrl.value); } } } if (options[OptListCtrlsMenus]) { list_controls(fd, 1); } if (options[OptListCtrls]) { list_controls(fd, 0); } close(fd); exit(app_result); }