summaryrefslogtreecommitdiff
path: root/cleopatre/application/libspid/src/system.c
blob: 7290b0cf38443f08d02bf4728bb2fbb0cec30412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* SPC300 bundle {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    application/libspid/src/system.c
 * \brief   system functions
 * \ingroup libspid
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "libspid.h"

/**
 * Give the current Linux OS version inside a provided buffer.
 *
 * \param buffer buffer where to put the result
 * \param buffer_len length of buffer where to put the result
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_NO_SPACE: no enough space in buffer
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */


libspid_error_t libspid_system_get_kernel_version(char *buffer, int buffer_len)
{
    FILE *fp;
    char line_buffer[LIBSPID_LINE_MAX_LEN];
    unsigned int len = 0;
    unsigned int total = 0;

    if (buffer == NULL || buffer_len <= 0)
        return LIBSPID_ERROR_PARAM;

    /* initialize buffer  - important for strcat to work properly */
    memset(buffer, 0x0, buffer_len);

    fp = fopen (LIBSPID_SYSTEM_VERSION_PATH, "r");
    if (fp != NULL)
    {
        while ( fgets(line_buffer, LIBSPID_LINE_MAX_LEN - 1, fp) )
        {
            /* check for buffer overload */
            len = strlen(line_buffer);
            total += len;
            if (total > buffer_len)
                return LIBSPID_ERROR_NO_SPACE;

            strcat(buffer, line_buffer);
        }

        fclose(fp);
    }
    else
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    return LIBSPID_SUCCESS;
}

/**
 * Give the current PLC driver version inside a provided buffer.
 *
 * \param buffer buffer where to put the result
 * \param buffer_len length of buffer where to put the result
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_NO_SPACE: no enough space in buffer
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_system_get_plc_version (char *buffer, const int buffer_len)
{
    return LIBSPID_SUCCESS;
}

/**
 * Give the current AV stack version inside a provided buffer.
 *
 * \param buffer buffer where to put the result
 * \param buffer_len length of buffer where to put the result
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_NO_SPACE: no enough space in buffer
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_system_get_av_version (char *buffer, const int buffer_len)
{
    return LIBSPID_SUCCESS;
}

/**
 * Give the time spent since the system has started.<BR>
 * Results are in 'seconds'.
 *
 * \param total_s total time in seconds since system start
 * \param idle_s total time of processor idle state since system
 * start
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_system_get_uptime(unsigned int *total_s, unsigned int *idle_s)
{
    FILE *fp;
    char buf[LIBSPID_LINE_MAX_LEN];
    int res;
    unsigned int uptotal = 0;
    unsigned int upidle = 0;
    unsigned int dummy1, dummy2; /* we use dummy vars to take decimal parts */

    if (total_s == NULL || idle_s == NULL)
        return LIBSPID_ERROR_PARAM;
    /*
       /proc/uptime    <--- we will read uptime info from this file
       This file contains information detailing how long the system
       has been on since its last restart. The output of /proc/uptime is quite minimal:

       350735.47 234388.90

       The first number is the total number of seconds the system has been up.
       The second number is how much of that time the machine has spent idle, in seconds.
     */
    fp = fopen (LIBSPID_SYSTEM_UPTIME_PATH, "r");
    if (fp != NULL)
    {
        if ( fgets(buf, LIBSPID_LINE_MAX_LEN - 1, fp) != NULL )
        {
            /* we cannot scanf %lf, we do not have double or float type on our ARM */
            res = sscanf (buf, "%d.%d %d.%d", &uptotal, &dummy1, &upidle, &dummy2);
            /* check if all 4 values were read */
            if (res != 4)
            {
                fclose(fp);
                return LIBSPID_ERROR_SYSTEM;
            }
        }

        fclose (fp);
    }

    *total_s = uptotal;
    *idle_s = upidle;

    return LIBSPID_SUCCESS;
}

/**
 * Get the current memory status of the system. All results are
 * put into the same provided buffer.
 *
 * \param buffer buffer where the result is put
 * \param buffer_len size of provided buffer
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 * \return LIBSPID_ERROR_NO_SPACE: not enough space in buffer
 */
libspid_error_t libspid_system_get_meminfo(char *buffer, int buffer_len)
{
    FILE *fp;
    char line_buffer[LIBSPID_LINE_MAX_LEN];
    unsigned int len = 0;
    unsigned int total = 0;

    if (buffer == NULL || buffer_len <= 0)
        return LIBSPID_ERROR_PARAM;

    /* initialize buffer  - important for strcat to work properly */
    memset(buffer, 0x0, buffer_len);

    fp = fopen (LIBSPID_SYSTEM_MEMINFO_PATH, "r");
    if (fp != NULL)
    {
        while ( fgets(line_buffer, LIBSPID_LINE_MAX_LEN - 1, fp) )
        {
            /* check for buffer overload */
            len = strlen(line_buffer);
            total += len;
            if (total > buffer_len)
                return LIBSPID_ERROR_NO_SPACE;

            strcat(buffer, line_buffer);
        }

        fclose (fp);
    }
    else
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    return LIBSPID_SUCCESS;
}

/**
 * Reboot the whole system.
 *
 */

void libspid_system_reboot(void)
{
    system("reboot");
}

/**
 * Save the system configuration files into the user ffs.
 *
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_system_save(void)
{
    FILE *fp;
    char *ptr, filename[128], command_buffer[256];
    int rc = 0;
    char prefix[16] = {0};

    if ( (fp = fopen(LIBSPID_SAVE_LIST_PATH, "r")) == NULL )
    {
        //syslog(LOG_WARNING, "libspid_save_config: cannot open %s (errno=%d)", LIBSPID_SAVE_LIST_PATH, errno);
        return LIBSPID_ERROR_SYSTEM;
    }
    /* check for /usr/local/etc validity */
    rc = system("mkdir -p /usr/local/etc");
    if (rc != 0)
    {
        fclose(fp);
        return LIBSPID_ERROR_SYSTEM;
    }

    while(fgets(filename, 127, fp))
    {
        if (strlen(filename) > 0)
        {
            filename[strlen(filename) - 1] = '\0';
        }
        /* check if the line in cnf file is /etc/<name> or just <name> */
        sprintf(prefix, "%s/", LIBSPID_CONF_ROOT_PATH);
        if ( !strncmp(filename, prefix, strlen(prefix)) )
            ptr = filename + strlen(prefix);
        else
            ptr = filename;
        if (0 == strlen(ptr))
        {
            continue;
        }
        sprintf(command_buffer, "cp -a %s/%.64s %s", LIBSPID_CONF_ROOT_PATH, ptr, LIBSPID_SAVE_DIR_PATH);
        //syslog(LOG_DEBUG, "libspid_save_config: %s", command_buffer);
        rc = system(command_buffer);
        if (rc !=0)
        {
            fclose(fp);
            return LIBSPID_ERROR_SYSTEM;
        }
    }
    fclose(fp);
    /* make sure that all *.conf files have been copied, even new ones not in save.lst */
    sprintf(command_buffer, "cp -a %s/*.conf %s", LIBSPID_CONF_ROOT_PATH, LIBSPID_SAVE_DIR_PATH);
    rc = system(command_buffer);
    if (rc !=0)
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    return LIBSPID_SUCCESS;
}

/**
 * Save a configuration file inside the user ffs.
 *
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad parameter
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_system_save_file(const char *filename)
{
    FILE *fp;
    char *ptr, command_buffer[256];
    int rc = 0;
    char prefix[16] = {0};

    if(filename == NULL)
    {
        return LIBSPID_ERROR_PARAM;
    }

    sprintf(command_buffer, "cp -a %s %s", filename, LIBSPID_SAVE_DIR_PATH);
    //syslog(LOG_DEBUG, "libspid_save_config: %s", command_buffer);
    rc = system(command_buffer);
    if (rc !=0)
    {
        return LIBSPID_ERROR_SYSTEM;
    }
    return LIBSPID_SUCCESS;
}

/**
 * Restore the system into the factory configuration.
 *
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_system_factory(void)
{
    int rc;
    char cmd[256] = {0};

    sprintf(cmd, "rm -rf %s/*", LIBSPID_SAVE_DIR_PATH);
    rc = system(cmd);
    if (rc !=0)
        return LIBSPID_ERROR_SYSTEM;

    sprintf(cmd, "cp -a %s/* %s", LIBSPID_FACTORY_PATH, LIBSPID_CONF_ROOT_PATH);
    rc = system(cmd);
    if (rc !=0)
        return LIBSPID_ERROR_SYSTEM;

    sprintf(cmd, "cp -a %s/* %s", LIBSPID_FACTORY_PATH, LIBSPID_SAVE_DIR_PATH);
    rc = system(cmd);
    if (rc !=0)
        return LIBSPID_ERROR_SYSTEM;

    //syslog("libspid_factory: returned to factory default");
    return LIBSPID_SUCCESS;
}

/**
 * Get the NVRAM content.
 *
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_NOT_FOUND: key not found or no more
 * line to read
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t libspid_system_get_nvram(spc300_nvram_t *nvram)
{
    FILE *fp;
    int fd;
    char *nvram_mtd_num;
    char nvram_mtd_path[128];
    char line_buffer[LIBSPID_LINE_MAX_LEN];
    char *strtok_ctx;
    int is_found = 0;

    if (nvram == NULL)
        return LIBSPID_ERROR_PARAM;

    fp = fopen (LIBSPID_SYSTEM_MTD_PATH, "r");
    if (fp != NULL)
    {
        while ( fgets(line_buffer, LIBSPID_LINE_MAX_LEN - 1, fp) != NULL )
        {
            if ( strstr(line_buffer, NVRAM_MTD_NAME) != NULL )
            {
                is_found = 1;
                /* Extract mtd path from line mtd<num>: <size> <erasesize> "nvram" */
                nvram_mtd_num = strtok_r(line_buffer, ": ", &strtok_ctx);
                sprintf(nvram_mtd_path, "%s/%s", LIBSPID_DEV_PATH, nvram_mtd_num);
            }
        }

        fclose (fp);

        if (is_found == 0)
        {
            return LIBSPID_ERROR_NOT_FOUND;
        }
    }
    else
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    /* open found mtd path using fd, i.e. with open sys call,
       read NVRAM magic string and verify (done below) */
    if ( (fd = open(nvram_mtd_path, O_RDONLY)) < 0 )
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    if( read(fd, nvram, sizeof(spc300_nvram_t)) < sizeof(spc300_nvram_t) )
    {
        close(fd);
        return LIBSPID_ERROR_SYSTEM;
    }
    close(fd);
    /* check NVRAM validity */
    if(!NVRAM_IS_VALID(nvram))
    {
       return LIBSPID_ERROR_SYSTEM;
    }

    return LIBSPID_SUCCESS;
}