Exemplo n.º 1
0
ssize_t settings_write_by_name(const char *name_p,
                               const void *src_p,
                               size_t size)
{
    ASSERTN(name_p != NULL, EINVAL);
    ASSERTN(src_p != NULL, EINVAL);
    ASSERTN(size > 0, EINVAL);

    const FAR struct setting_t *setting_p;

    /* Find the setting in the settings array. */
    setting_p = &settings[0];

    while (setting_p->name_p != NULL) {
        if (std_strcmp(name_p, setting_p->name_p) == 0) {
            if (size > setting_p->size) {
                return (-1);
            }

            return (settings_write(setting_p->address, src_p, size));
        }

        setting_p++;
    }

    return (-1);
}
Exemplo n.º 2
0
int settings_read(void) {
    FILE *fp;
    char *content;
    size_t bytes;
    JsonNode *root;
    struct stat st;

    /* Read JSON config file */
    if(!(fp = fopen(settingsfile, "rb"))) {
        logprintf(LOG_ERR, "cannot read settings file: %s", settingsfile);
        return EXIT_FAILURE;
    }

    fstat(fileno(fp), &st);
    bytes = (size_t)st.st_size;

    if(!(content = calloc(bytes+1, sizeof(char)))) {
        logprintf(LOG_ERR, "out of memory");
        fclose(fp);
        return EXIT_FAILURE;
    }

    if(fread(content, sizeof(char), bytes, fp) == -1) {
        logprintf(LOG_ERR, "cannot read settings file: %s", settingsfile);
    }
    fclose(fp);

    /* Validate JSON and turn into JSON object */
    if(json_validate(content) == false) {
        logprintf(LOG_ERR, "settings are not in a valid json format", content);
        sfree((void *)&content);
        return EXIT_FAILURE;
    }

    root = json_decode(content);

    if(settings_parse(root) != 0) {
        sfree((void *)&content);
        return EXIT_FAILURE;
    }
    char *output = json_stringify(root, "\t");
    settings_write(output);
    json_delete(root);
    sfree((void *)&output);
    sfree((void *)&content);
    return EXIT_SUCCESS;
}
Exemplo n.º 3
0
void cleanup(void)
{
    msg_log(MSG_MESSAGE, "Cleanup...\n");
    session_cleanup();
    dish_file_state_cleanup();
    midi_stop();
    driver_stop();
    patch_shutdown();
    mixer_shutdown();
    settings_write();
    settings_free();
    free_instance_name();
    mod_src_destroy();

    msg_log(MSG_MESSAGE, "Goodbye!\n");

    exit(0);
}
Exemplo n.º 4
0
void settings_reset() {
	settings = (Settings)SETTINGS_DEFAULTS;
	preset = (Preset)PRESET_DEFAULTS;
	settings_write();
}
Exemplo n.º 5
0
// Main page CGI handler
static void httpd_main_cgi_handler( char *pars )
{
  char *p, *arg, *val;
  SETTINGS new_settings;

  new_settings = *settings_get();
  new_settings.static_mode = 1;
  p = pars;
  httpd_resp_buf[ 0 ] = '\0';
  while( 1 )  
  {
    // Get a single argument/value pair
    p = httpd_get_cgi_data( p, &arg, &val );

    if( arg[ 0 ] && val[ 0 ] )
    {
      // Process this pair
      if( strcmp( arg, "sip" ) == 0 ) // source ip
      {
        if( utils_string_to_ip( val, &new_settings.ip ) == 0 )
        {
          strcpy( httpd_last_err, "Invalid source IP" );
          return;
        }
      }

      else if( strcmp( arg, "gwip" ) == 0 ) // gateway
      {
        if( utils_string_to_ip( val, &new_settings.gw ) == 0 )
        {
          strcpy( httpd_last_err, "Invalid gateway IP" );
          return;
        }
      }

      else if( strcmp( arg, "sn" ) == 0 ) // netmask
      {
        if( utils_string_to_ip( val, &new_settings.mask ) == 0 )
        {
          strcpy( httpd_last_err, "Invalid network mask" );
          return;
        }
      }

      else if( strcmp( arg, "dns" ) == 0 ) // dns
      {
        if( utils_string_to_ip( val, &new_settings.dns ) == 0 )
        {
          strcpy( httpd_last_err, "Invalid DNS IP" );
          return;
        }
      }

      else if( strcmp( arg, "hwaddr" ) == 0 ) // MAC
      {
        if( utils_string_to_mac( val, &new_settings.mac ) == 0 )
        {
          strcpy( httpd_last_err, "Invalid MAC address" );
          return;
        }
      } 
      
      else if( strcmp( arg, "isdhcp" ) == 0 && strcmp( val, "on" ) == 0 )
        new_settings.static_mode = 0;
        
      else if( strcmp( arg, "bname" ) == 0 )
      {
        new_settings.name[ SETTINGS_MAX_NAME_LEN ] = '\0';
        strncpy( new_settings.name, val, SETTINGS_MAX_NAME_LEN );
      }

      else if( strcmp( arg, "username" ) == 0 )
      {
        new_settings.username[ SETTINGS_MAX_USERNAME_LEN ] = '\0';
        strncpy( new_settings.username, val, SETTINGS_MAX_USERNAME_LEN );
      }

      else if( strcmp( arg, "password" ) == 0 )
      {
        new_settings.password[ SETTINGS_MAX_PASSWORD_LEN ] = '\0';
        strncpy( new_settings.password, val, SETTINGS_MAX_PASSWORD_LEN );
      }
        
      else 
      {
        strcpy( httpd_last_err, "Invalid argument found in CGI list" );
        return;
      }           
    }

    if( p == NULL )
      break;
  }
  settings_set( &new_settings );
  settings_write();
  strcpy( httpd_last_err, "Configuration updated" );
}
Exemplo n.º 6
0
static int cmd_write_cb(int argc,
                        const char *argv[],
                        void *chout_p,
                        void *chin_p,
                        void *arg_p,
                        void *call_arg_p)
{
    const FAR struct setting_t *setting_p;
    long value;
    int8_t int8;
    int16_t int16;
    int32_t int32;

    if (argc != 3) {
        std_fprintf(chout_p, FSTR("Usage: %s <name> <value>\r\n"), argv[0]);

        return (-1);
    }

    /* Find the setting in the settings array. */
    setting_p = &settings[0];

    while (setting_p->name_p != NULL) {
        if (std_strcmp(argv[1], setting_p->name_p) == 0) {
            switch (setting_p->type) {

            case setting_type_int8_t:
                if (std_strtol(argv[2], &value) == NULL) {
                    return (-1);
                }

                /* Range check. */
                if ((value > 127) || (value < -128)) {
                    std_fprintf(chout_p,
                                FSTR("%ld: value out of range\r\n"),
                                value);
                    return (-1);
                }

                int8 = (int8_t)value;
                settings_write(setting_p->address, &int8, setting_p->size);
                break;

            case setting_type_int16_t:
                if (std_strtol(argv[2], &value) == NULL) {
                    return (-1);
                }

                /* Range check. */
                if ((value > 32767) || (value < -32768)) {
                    std_fprintf(chout_p,
                                FSTR("%ld: value out of range\r\n"),
                                value);
                    return (-1);
                }

                int16 = (int16_t)value;
                settings_write(setting_p->address, &int16, setting_p->size);
                break;

            case setting_type_int32_t:
                if (std_strtol(argv[2], &value) == NULL) {
                    return (-1);
                }

                /* Range check. */
                if ((value > 2147483647) || (value < -2147483648)) {
                    std_fprintf(chout_p,
                                FSTR("%ld: value out of range\r\n"),
                                value);
                    return (-1);
                }

                int32 = (int32_t)value;
                settings_write(setting_p->address, &int32, setting_p->size);
                break;

            case setting_type_string_t:
                /* Range check. */
                if (strlen(argv[2]) >= setting_p->size) {
                    std_fprintf(chout_p,
                                FSTR("%s: string too long\r\n"),
                                argv[2]);
                    return (-1);
                }

                settings_write(setting_p->address, argv[2], setting_p->size);
                break;

            default:
                std_fprintf(chout_p,
                            FSTR("bad setting type %d\r\n"),
                            setting_p->type);
            }

            return (0);
        }

        setting_p++;
    }

    std_fprintf(chout_p, FSTR("%s: setting not found\r\n"), argv[1]);

    return (-1);
}