Add new TLV socket project
New file |
| | |
| | | /*
|
| | | * crc-itu-t.c
|
| | | *
|
| | | * This source code is licensed under the GNU General Public License,
|
| | | * Version 2. See the file COPYING for more details.
|
| | | */
|
| | |
|
| | | #include "crc-itu-t.h"
|
| | |
|
| | | /** CRC table for the CRC ITU-T V.41 0x0x1021 (x^16 + x^12 + x^15 + 1) */
|
| | | const unsigned short crc_itu_t_table[256] = {
|
| | | 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
|
| | | 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
|
| | | 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
|
| | | 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
|
| | | 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
|
| | | 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
|
| | | 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
|
| | | 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
|
| | | 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
|
| | | 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
|
| | | 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
|
| | | 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
|
| | | 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
|
| | | 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
|
| | | 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
|
| | | 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
|
| | | 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
|
| | | 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
|
| | | 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
|
| | | 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
|
| | | 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
|
| | | 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
|
| | | 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
|
| | | 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
|
| | | 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
|
| | | 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
|
| | | 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
|
| | | 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
|
| | | 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
|
| | | 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
|
| | | 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
|
| | | 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
|
| | | };
|
| | |
|
| | | /**
|
| | | * crc_itu_t - Compute the CRC-ITU-T for the data buffer
|
| | | *
|
| | | * @crc: previous CRC value
|
| | | * @buffer: data pointer
|
| | | * @len: number of bytes in the buffer
|
| | | *
|
| | | * Returns the updated CRC value
|
| | | */
|
| | | unsigned short crc_itu_t(unsigned short crc, const unsigned char *buffer, unsigned int len)
|
| | | {
|
| | | while (len--)
|
| | | crc = crc_itu_t_byte(crc, *buffer++);
|
| | | return crc;
|
| | | }
|
| | |
|
| | | int ushort_to_bytes(unsigned char *bytes, unsigned short val)
|
| | | {
|
| | | int size = sizeof (unsigned short);
|
| | | int i = 0;
|
| | |
|
| | | if( !bytes )
|
| | | return 0;
|
| | |
|
| | | while (size)
|
| | | {
|
| | | *(bytes + --size) = (val >> ((8 * i++) & 0xFF));
|
| | | }
|
| | |
|
| | | return sizeof(unsigned short);
|
| | | }
|
| | |
|
| | | unsigned short bytes_to_ushort(unsigned char *bytes, int len)
|
| | | {
|
| | | int i = 0;
|
| | | unsigned short val = 0;
|
| | |
|
| | | if( !bytes || len > sizeof(unsigned short) )
|
| | | return 0;
|
| | |
|
| | | for(i=0; i<len; i++)
|
| | | {
|
| | | val += bytes[i];
|
| | |
|
| | | if (i < len - 1)
|
| | | val = val << 8;
|
| | | }
|
| | |
|
| | | return val;
|
| | | }
|
New file |
| | |
| | | /*
|
| | | * crc-itu-t.h - CRC ITU-T V.41 routine
|
| | | *
|
| | | * Implements the standard CRC ITU-T V.41:
|
| | | * Width 16
|
| | | * Poly 0x0x1021 (x^16 + x^12 + x^15 + 1)
|
| | | * Init 0
|
| | | *
|
| | | * This source code is licensed under the GNU General Public License,
|
| | | * Version 2. See the file COPYING for more details.
|
| | | */
|
| | |
|
| | | #ifndef CRC_ITU_T_H
|
| | | #define CRC_ITU_T_H
|
| | |
|
| | | #define MAGIC_CRC 0x1E50
|
| | |
|
| | | extern const unsigned short crc_itu_t_table[256];
|
| | |
|
| | |
|
| | | /**
|
| | | * crc_itu_t - Compute the CRC-ITU-T for the data buffer
|
| | | *
|
| | | * @crc: previous CRC value, we use $IoT_MAGIC_CRC
|
| | | * @buffer: data pointer
|
| | | * @len: number of bytes in the buffer
|
| | | *
|
| | | * Returns the updated CRC value
|
| | | */
|
| | | extern unsigned short crc_itu_t(unsigned short magic_crc, const unsigned char *buffer, unsigned int len);
|
| | |
|
| | |
|
| | | /*
|
| | | * ushort_to_bytes - Convert unsigned short CRC16 value to two bytes
|
| | | * @bytes: two bytes CRC16 value saving buffer, buffer size must more than 2 bytes
|
| | | * @crc: crc_itu_t() compute returnd value( unsigned short CRC16 checksum)
|
| | | */
|
| | | extern int ushort_to_bytes(unsigned char *bytes, unsigned short val);
|
| | |
|
| | |
|
| | | /*
|
| | | * bytes_to_ushort - Convert two bytes mode CRC value into unsigned short CRC16 value |
| | | * @bytes: bytes mode CRC value first byte position
|
| | | * @len: bytes mode CRC value length
|
| | | */
|
| | | extern unsigned short bytes_to_ushort(unsigned char *bytes, int len);
|
| | |
|
| | |
|
| | | static inline unsigned short crc_itu_t_byte(unsigned short crc, const unsigned char data)
|
| | | {
|
| | | return (crc << 8) ^ crc_itu_t_table[((crc >> 8) ^ data) & 0xff];
|
| | | }
|
| | |
|
| | | #endif /* CRC_ITU_T_H */
|
| | |
|
New file |
| | |
| | | /********************************************************************************* |
| | | * Copyright: (C) 2012 Guo Wenxue <guowenxue@gmail.com> |
| | | * All rights reserved. |
| | | * |
| | | * Filename: logger.c |
| | | * Description: This file is the linux infrastructural logger system library |
| | | * |
| | | * Version: 1.0.0(08/08/2012~) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "08/08/2012 04:24:01 PM" |
| | | * |
| | | ********************************************************************************/ |
| | | |
| | | #include "logger.h" |
| | | |
| | | #define PRECISE_TIME_FACTOR 1000 |
| | | |
| | | static unsigned long log_rollback_size = LOG_ROLLBACK_NONE; |
| | | |
| | | static logger_t *logger = NULL; |
| | | |
| | | char *log_str[LOG_LEVEL_MAX + 1] = { "", "F", "E", "W", "N", "D", "I", "T", "M" }; |
| | | |
| | | #define LOG_TIME_FMT "%Y-%m-%d %H:%M:%S" |
| | | |
| | | static void log_signal_handler(int sig) |
| | | { |
| | | if(!logger) |
| | | return ; |
| | | |
| | | if (sig == SIGHUP) |
| | | { |
| | | signal(SIGHUP, log_signal_handler); |
| | | log_fatal("SIGHUP received - reopenning log file [%s]", logger->file); |
| | | logger_reopen(); |
| | | } |
| | | } |
| | | |
| | | static void logger_banner(char *prefix) |
| | | { |
| | | if(!logger) |
| | | return ; |
| | | |
| | | fprintf(logger->fp, "%s log \"%s\" on level [%s] size [%lu] KiB, log system version %s\n", |
| | | prefix, logger->file, log_str[logger->level], log_rollback_size / 1024, LOG_VERSION_STR); |
| | | #ifdef LOG_FILE_LINE |
| | | fprintf(logger->fp, " [ Date ] [ Time ] [ Level ] [ File/Line ] [ Message ]\n"); |
| | | #else |
| | | fprintf(logger->fp, " [ Date ] [ Time ] [ Level ] [ Message ]\n"); |
| | | #endif |
| | | fprintf(logger->fp, "-------------------------------------------------------------\n"); |
| | | } |
| | | |
| | | static void check_and_rollback(void) |
| | | { |
| | | if(!logger) |
| | | return ; |
| | | |
| | | if (log_rollback_size != LOG_ROLLBACK_NONE) |
| | | { |
| | | long _curOffset = ftell(logger->fp); |
| | | |
| | | if ((_curOffset != -1) && (_curOffset >= log_rollback_size)) |
| | | { |
| | | char cmd[512]; |
| | | |
| | | snprintf(cmd, sizeof(cmd), "cp -f %s %s.roll", logger->file, logger->file); |
| | | system(cmd); |
| | | |
| | | if (-1 == fseek(logger->fp, 0L, SEEK_SET)) |
| | | fprintf(logger->fp, "log rollback fseek failed \n"); |
| | | |
| | | rewind(logger->fp); |
| | | |
| | | truncate(logger->file, 0); |
| | | logger_banner("Already rollback"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /* log_size unit is KB */ |
| | | int logger_init(logger_t *log, char *log_file, int level, int log_size) |
| | | { |
| | | if( !log ) |
| | | { |
| | | printf("ERROR: Invalid input arguments\n"); |
| | | return -1; |
| | | } |
| | | |
| | | if( log_file ) |
| | | { |
| | | strncpy(log->file, log_file, FILENAME_LEN); |
| | | log->flag |= FLAG_LOGGER_FILE; |
| | | } |
| | | else |
| | | { |
| | | strncpy(log->file, DBG_LOG_FILE, FILENAME_LEN); |
| | | log->flag |= FLAG_LOGGER_CONSOLE; |
| | | } |
| | | |
| | | log->level = level; |
| | | log->size = log_size; |
| | | |
| | | /* set static global $logger point to argument $log */ |
| | | logger = log; |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | int logger_open(void) |
| | | { |
| | | struct sigaction act; |
| | | char *filemode; |
| | | |
| | | if(!logger) |
| | | { |
| | | return -1; |
| | | } |
| | | |
| | | log_rollback_size = logger->size <= 0 ? LOG_ROLLBACK_NONE : logger->size*1024; /* Unit KiB */ |
| | | |
| | | if ('\0' == logger->file) |
| | | return -1; |
| | | |
| | | if (!strcmp(logger->file, DBG_LOG_FILE)) |
| | | { |
| | | logger->fp = stderr; |
| | | log_rollback_size = LOG_ROLLBACK_NONE; |
| | | logger->flag |= FLAG_LOGGER_CONSOLE; |
| | | goto OUT; |
| | | } |
| | | |
| | | filemode = (log_rollback_size==LOG_ROLLBACK_NONE) ? "a+" : "w+"; |
| | | |
| | | logger->fp = fopen(logger->file, filemode); |
| | | if (NULL == logger->fp) |
| | | { |
| | | fprintf(stderr, "Open log file \"%s\" in %s failure\n", logger->file, filemode); |
| | | return -2; |
| | | } |
| | | |
| | | act.sa_handler = log_signal_handler; |
| | | sigemptyset(&act.sa_mask); |
| | | act.sa_flags = 0; |
| | | sigaction(SIGHUP, &act, NULL); |
| | | |
| | | OUT: |
| | | logger_banner("Initialize"); |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | void logger_close(void) |
| | | { |
| | | if (!logger || !logger->fp ) |
| | | return; |
| | | |
| | | logger_banner("\nTerminate"); |
| | | logger_raw("\n\n\n\n"); |
| | | |
| | | fflush(logger->fp); |
| | | |
| | | fclose(logger->fp); |
| | | logger->fp = NULL; |
| | | |
| | | return ; |
| | | } |
| | | |
| | | int logger_reopen(void) |
| | | { |
| | | int rc = 0; |
| | | char *filemode; |
| | | |
| | | if( !logger ) |
| | | return -1; |
| | | |
| | | log_rollback_size = logger->size <= 0 ? LOG_ROLLBACK_NONE : logger->size*1024; /* Unit KiB */ |
| | | |
| | | if (logger->flag & FLAG_LOGGER_CONSOLE ) |
| | | { |
| | | fflush(logger->fp); |
| | | logger->fp = stderr; |
| | | return 0; |
| | | } |
| | | |
| | | if (logger->fp) |
| | | { |
| | | logger_close(); |
| | | filemode = log_rollback_size==LOG_ROLLBACK_NONE ? "a+" : "w+"; |
| | | logger->fp = fopen(logger->file, filemode); |
| | | |
| | | if (logger->fp == NULL) |
| | | rc = -2; |
| | | } |
| | | else |
| | | { |
| | | rc = -3; |
| | | } |
| | | |
| | | if (!rc) |
| | | { |
| | | logger_banner("\nReopen"); |
| | | } |
| | | return rc; |
| | | } |
| | | |
| | | void logger_term(void) |
| | | { |
| | | if(!logger) |
| | | return ; |
| | | |
| | | logger_close(); |
| | | |
| | | logger = NULL; |
| | | } |
| | | |
| | | void logger_raw(const char *fmt, ...) |
| | | { |
| | | va_list argp; |
| | | |
| | | if (!logger || !logger->fp) |
| | | return; |
| | | |
| | | check_and_rollback(); |
| | | |
| | | va_start(argp, fmt); |
| | | vfprintf(logger->fp, fmt, argp); |
| | | va_end(argp); |
| | | } |
| | | |
| | | static void cp_printout(char *level, char *fmt, va_list argp) |
| | | { |
| | | char buf[MAX_LOG_MESSAGE_LEN]; |
| | | struct tm *local; |
| | | struct timeval now; |
| | | char timestr[256]; |
| | | |
| | | if(!logger) |
| | | return ; |
| | | |
| | | check_and_rollback(); |
| | | |
| | | gettimeofday(&now, NULL); |
| | | local = localtime(&now.tv_sec); |
| | | |
| | | strftime(timestr, 256, LOG_TIME_FMT, local); |
| | | vsnprintf(buf, MAX_LOG_MESSAGE_LEN, fmt, argp); |
| | | |
| | | #ifdef DUMPLICATE_OUTPUT |
| | | printf("%s.%03ld [%s] : %s", |
| | | timestr, now.tv_usec / PRECISE_TIME_FACTOR, level, buf); |
| | | #endif |
| | | |
| | | if (logger->fp) |
| | | fprintf(logger->fp, "%s.%03ld [%s] : %s", timestr, now.tv_usec / PRECISE_TIME_FACTOR, level, buf); |
| | | |
| | | if (logger->fp) |
| | | fflush(logger->fp); |
| | | } |
| | | |
| | | static void cp_printout_line(char *level, char *fmt, char *file, int line, va_list argp) |
| | | { |
| | | char buf[MAX_LOG_MESSAGE_LEN]; |
| | | struct tm *local; |
| | | struct timeval now; |
| | | char timestr[256]; |
| | | |
| | | if(!logger) |
| | | return ; |
| | | |
| | | check_and_rollback(); |
| | | |
| | | gettimeofday(&now, NULL); |
| | | local = localtime(&now.tv_sec); |
| | | |
| | | strftime(timestr, 256, LOG_TIME_FMT, local); |
| | | vsnprintf(buf, MAX_LOG_MESSAGE_LEN, fmt, argp); |
| | | |
| | | #ifdef DUMPLICATE_OUTPUT |
| | | printf("[%s.%03ld] <%s> <%s:%04d> : %s", |
| | | timestr, now.tv_usec / PRECISE_TIME_FACTOR, level, file, line, buf); |
| | | #endif |
| | | |
| | | if (logger->fp) |
| | | { |
| | | fprintf(logger->fp, "[%s.%03ld] <%s> <%s:%04d> : %s", |
| | | timestr, now.tv_usec / PRECISE_TIME_FACTOR, level, file, line, buf); |
| | | |
| | | fflush(logger->fp); |
| | | } |
| | | } |
| | | |
| | | void logger_str(int level, const char *msg) |
| | | { |
| | | if (!logger || level>logger->level) |
| | | return; |
| | | |
| | | check_and_rollback(); |
| | | |
| | | if (logger->fp) |
| | | fwrite(msg, 1, strlen(msg), logger->fp); |
| | | |
| | | if(logger->fp) |
| | | fflush(logger->fp); |
| | | } |
| | | |
| | | void logger_min(int level, char *fmt, ...) |
| | | { |
| | | va_list argp; |
| | | |
| | | if (!logger || level>logger->level) |
| | | return; |
| | | |
| | | va_start(argp, fmt); |
| | | cp_printout(log_str[level], fmt, argp); |
| | | va_end(argp); |
| | | } |
| | | |
| | | void logger_line(int level, char *file, int line, char *fmt, ...) |
| | | { |
| | | va_list argp; |
| | | |
| | | if (!logger || level>logger->level) |
| | | return; |
| | | |
| | | va_start(argp, fmt); |
| | | cp_printout_line(log_str[level], fmt, file, line, argp); |
| | | |
| | | va_end(argp); |
| | | } |
| | | |
| | | #define LINELEN 81 |
| | | #define CHARS_PER_LINE 16 |
| | | static char *print_char = |
| | | " " |
| | | " " |
| | | " !\"#$%&'()*+,-./" |
| | | "0123456789:;<=>?" |
| | | "@ABCDEFGHIJKLMNO" |
| | | "PQRSTUVWXYZ[\\]^_" |
| | | "`abcdefghijklmno" |
| | | "pqrstuvwxyz{|}~ " |
| | | " " |
| | | " " |
| | | " ???????????????" |
| | | "????????????????" |
| | | "????????????????" |
| | | "????????????????" |
| | | "????????????????" |
| | | "????????????????"; |
| | | |
| | | void logger_dump(int level, char *buf, int len) |
| | | { |
| | | int rc; |
| | | int idx; |
| | | char prn[LINELEN]; |
| | | char lit[CHARS_PER_LINE + 2]; |
| | | char hc[4]; |
| | | short line_done = 1; |
| | | |
| | | if (!logger || level>logger->level) |
| | | return; |
| | | |
| | | rc = len; |
| | | idx = 0; |
| | | lit[CHARS_PER_LINE] = '\0'; |
| | | |
| | | while (rc > 0) |
| | | { |
| | | if (line_done) |
| | | snprintf(prn, LINELEN, "%08X: ", idx); |
| | | |
| | | do |
| | | { |
| | | unsigned char c = buf[idx]; |
| | | snprintf(hc, 4, "%02X ", c); |
| | | strncat(prn, hc, LINELEN); |
| | | |
| | | lit[idx % CHARS_PER_LINE] = print_char[c]; |
| | | } |
| | | while (--rc > 0 && (++idx % CHARS_PER_LINE != 0)); |
| | | |
| | | line_done = (idx % CHARS_PER_LINE) == 0; |
| | | if (line_done) |
| | | { |
| | | #ifdef DUMPLICATE_OUTPUT |
| | | printf("%s %s\n", prn, lit); |
| | | #endif |
| | | if (logger->fp) |
| | | fprintf(logger->fp, "%s %s\n", prn, lit); |
| | | } |
| | | } |
| | | |
| | | if (!line_done) |
| | | { |
| | | int ldx = idx % CHARS_PER_LINE; |
| | | lit[ldx++] = print_char[(int)buf[idx]]; |
| | | lit[ldx] = '\0'; |
| | | |
| | | while ((++idx % CHARS_PER_LINE) != 0) |
| | | strncat(prn, " ", LINELEN); |
| | | |
| | | #ifdef DUMPLICATE_OUTPUT |
| | | printf("%s %s\n", prn, lit); |
| | | #endif |
| | | if (logger->fp) |
| | | fprintf(logger->fp, "%s %s\n", prn, lit); |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | /******************************************************************************** |
| | | * Copyright: (C) 2012 Guo Wenxue <guowenxue@gmail.com> |
| | | * All rights reserved. |
| | | * |
| | | * Filename: logger.h |
| | | * Description: This file is the linux infrastructural logger system library |
| | | * |
| | | * Version: 1.0.0(08/08/2012~) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "08/08/2012 05:16:56 PM" |
| | | * |
| | | ********************************************************************************/ |
| | | |
| | | #ifndef _LOGGER_H_ |
| | | #define _LOGGER_H_ |
| | | |
| | | #include <stdarg.h> |
| | | #include <stdio.h> |
| | | #include <stdlib.h> |
| | | #include <string.h> |
| | | #include <unistd.h> |
| | | #include <signal.h> |
| | | #include <time.h> |
| | | #include <errno.h> |
| | | |
| | | #include <sys/types.h> |
| | | #include <sys/time.h> |
| | | |
| | | #define LOG_VERSION_STR "1.0.0" |
| | | |
| | | #ifndef FILENAME_LEN |
| | | #define FILENAME_LEN 64 |
| | | #endif |
| | | |
| | | #define DBG_LOG_FILE "stderr" /* Debug mode log file is console */ |
| | | |
| | | #define LOG_ROLLBACK_SIZE 512 /* Default rollback log size */ |
| | | #define LOG_ROLLBACK_NONE 0 /* Set rollback size to 0 will not rollback */ |
| | | |
| | | #define DEFAULT_TIME_FORMAT "%Y-%m-%d %H:%M:%S" |
| | | #define MAX_LOG_MESSAGE_LEN 0x1000 |
| | | |
| | | //#define DUMPLICATE_OUTPUT /* Log to file and printf on console */ |
| | | |
| | | enum |
| | | { |
| | | LOG_LEVEL_DISB = 0, /* Disable "Debug" */ |
| | | LOG_LEVEL_FATAL, /* Debug Level "Fatal" */ |
| | | LOG_LEVEL_ERROR, /* Debug Level "ERROR" */ |
| | | LOG_LEVEL_WARN, /* Debug Level "warnning" */ |
| | | LOG_LEVEL_NRML, /* Debug Level "Normal" */ |
| | | LOG_LEVEL_DEBUG, /* Debug Level "Debug" */ |
| | | LOG_LEVEL_INFO, /* Debug Level "Information" */ |
| | | LOG_LEVEL_TRACE, /* Debug Level "Trace" */ |
| | | LOG_LEVEL_MAX, |
| | | }; |
| | | |
| | | |
| | | |
| | | /* logger->flag definition */ |
| | | #define FLAG_LOGGER_LEVEL_OPT 1<<0 /* The log level is sepcified by the command option */ |
| | | |
| | | #define FLAG_LOGGER_CONSOLE 1<<1 |
| | | #define FLAG_LOGGER_FILE 0<<1 |
| | | |
| | | typedef struct logger_s |
| | | { |
| | | unsigned char flag; |
| | | char file[FILENAME_LEN]; |
| | | int level; |
| | | int size; |
| | | |
| | | FILE *fp; |
| | | } logger_t; |
| | | |
| | | extern char *log_str[]; |
| | | |
| | | /* log_size unit is KB */ |
| | | extern int logger_init(logger_t *logger, char *filename, int level, int log_size); |
| | | extern int logger_open(void); |
| | | extern void logger_set_time_format(char *time_format); |
| | | extern int logger_reopen(void); |
| | | extern void logger_close(void); |
| | | extern void logger_term(void); |
| | | extern void logger_raw(const char *fmt, ...); |
| | | |
| | | extern void logger_min(int level, char *fmt, ...); |
| | | extern void logger_line(int level, char *file, int line, char *fmt, ...); |
| | | extern void logger_str(int level, const char *msg); |
| | | extern void logger_dump(int level, char *buf, int len); |
| | | |
| | | #define LOG_FILE_LINE /* Log the file and line */ |
| | | |
| | | #ifdef LOG_FILE_LINE |
| | | #define log_trace(fmt, ...) logger_line(LOG_LEVEL_TRACE, __FILE__, __LINE__, fmt, ##__VA_ARGS__) |
| | | #define log_info(fmt, ...) logger_line(LOG_LEVEL_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__) |
| | | #define log_dbg(fmt, ...) logger_line(LOG_LEVEL_DEBUG, __FILE__, __LINE__, fmt, ##__VA_ARGS__) |
| | | #define log_nrml(fmt, ...) logger_line(LOG_LEVEL_NRML, __FILE__, __LINE__, fmt, ##__VA_ARGS__) |
| | | #define log_warn(fmt, ...) logger_line(LOG_LEVEL_WARN, __FILE__, __LINE__, fmt, ##__VA_ARGS__) |
| | | #define log_err(fmt, ...) logger_line(LOG_LEVEL_ERROR, __FILE__, __LINE__, fmt, ##__VA_ARGS__) |
| | | #define log_fatal(fmt, ...) logger_line(LOG_LEVEL_FATAL, __FILE__, __LINE__, fmt, ##__VA_ARGS__) |
| | | #else |
| | | #define log_trace(fmt, ...) logger_min(LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__) |
| | | #define log_info(fmt, ...) logger_min(LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) |
| | | #define log_dbg(fmt, ...) logger_min(LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__) |
| | | #define log_nrml(fmt, ...) logger_min(LOG_LEVEL_NRML, fmt, ##__VA_ARGS__) |
| | | #define log_warn(fmt, ...) logger_min(LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) |
| | | #define log_err(fmt, ...) logger_min(LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) |
| | | #define log_fatal(fmt, ...) logger_min(LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__) |
| | | #endif |
| | | |
| | | #endif /* ----- #ifndef _LOGGER_H_ ----- */ |
| | | |
New file |
| | |
| | | #LIBNAME=$(shell basename ${PWD}) |
| | | LIBNAME=lylib |
| | | |
| | | CC=${CROSSTOOL}gcc |
| | | LD=${CROSSTOOL}ld |
| | | AS=${CROSSTOOL}as |
| | | AR=${CROSSTOOL}ar |
| | | |
| | | all: liba |
| | | @rm -f *.o |
| | | |
| | | libso: |
| | | ${CC} -fPIC -shared *.c -o lib${LIBNAME}.so |
| | | |
| | | liba: |
| | | @${CC} -c *.c |
| | | ${AR} -rcs lib${LIBNAME}.a *.o |
| | | |
| | | clean: |
| | | rm -f *.a *.so |
| | | |
| | | distclean: clean |
| | | rm -f *.o |
New file |
| | |
| | | /********************************************************************************* |
| | | * Copyright: (C) 2012 Guo Wenxue<guowenxue@gmail.com> |
| | | * All rights reserved. |
| | | * |
| | | * Filename: proc.c |
| | | * Description: This file is the process API |
| | | * |
| | | * Version: 1.0.0(11/06/2012~) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "11/06/2012 09:19:02 PM" |
| | | * |
| | | ********************************************************************************/ |
| | | |
| | | #include <stdio.h> |
| | | #include <stdlib.h> |
| | | #include <unistd.h> |
| | | #include <libgen.h> |
| | | #include <fcntl.h> |
| | | #include <sys/types.h> |
| | | #include <sys/stat.h> |
| | | #include <pthread.h> |
| | | |
| | | #include "proc.h" |
| | | #include "logger.h" |
| | | |
| | | proc_signal_t g_signal={0}; |
| | | |
| | | void proc_sighandler(int sig) |
| | | { |
| | | switch(sig) |
| | | { |
| | | case SIGINT: |
| | | log_warn("SIGINT - stopping\n"); |
| | | g_signal.stop = 1; |
| | | break; |
| | | |
| | | case SIGTERM: |
| | | log_warn("SIGTERM - stopping\n"); |
| | | g_signal.stop = 1; |
| | | break; |
| | | case SIGSEGV: |
| | | log_warn("SIGSEGV - stopping\n"); |
| | | break; |
| | | |
| | | case SIGPIPE: |
| | | log_warn("SIGPIPE - warnning\n"); |
| | | break; |
| | | |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | |
| | | void install_proc_signal(void) |
| | | { |
| | | struct sigaction sigact, sigign; |
| | | |
| | | log_nrml("Install default signal handler.\n"); |
| | | |
| | | /* Initialize the catch signal structure. */ |
| | | sigemptyset(&sigact.sa_mask); |
| | | sigact.sa_flags = 0; |
| | | sigact.sa_handler = proc_sighandler; |
| | | |
| | | /* Setup the ignore signal. */ |
| | | sigemptyset(&sigign.sa_mask); |
| | | sigign.sa_flags = 0; |
| | | sigign.sa_handler = SIG_IGN; |
| | | |
| | | sigaction(SIGTERM, &sigact, 0); /* catch terminate signal "kill" command */ |
| | | sigaction(SIGINT, &sigact, 0); /* catch interrupt signal CTRL+C */ |
| | | //sigaction(SIGSEGV, &sigact, 0); /* catch segmentation faults */ |
| | | sigaction(SIGPIPE, &sigact, 0); /* catch broken pipe */ |
| | | #if 0 |
| | | sigaction(SIGCHLD, &sigact, 0); /* catch child process return */ |
| | | sigaction(SIGUSR2, &sigact, 0); /* catch USER signal */ |
| | | #endif |
| | | } |
| | | |
| | | |
| | | /* **************************************************************************** |
| | | * FunctionName: daemonize |
| | | * Description : Set the programe runs as daemon in background |
| | | * Inputs : nodir: DON'T change the work directory to / : 1:NoChange 0:Change |
| | | * noclose: close the opened file descrtipion or not 1:Noclose 0:Close |
| | | * Output : NONE |
| | | * Return : NONE |
| | | * *****************************************************************************/ |
| | | void daemonize(int nochdir, int noclose) |
| | | { |
| | | int retval, fd; |
| | | int i; |
| | | |
| | | /* already a daemon */ |
| | | if (1 == getppid()) |
| | | return; |
| | | |
| | | /* fork error */ |
| | | retval = fork(); |
| | | if (retval < 0) exit(1); |
| | | |
| | | /* parent process exit */ |
| | | if (retval > 0) |
| | | exit(0); |
| | | |
| | | /* obtain a new process session group */ |
| | | setsid(); |
| | | |
| | | if (!noclose) |
| | | { |
| | | /* close all descriptors */ |
| | | for (i = getdtablesize(); i >= 0; --i) |
| | | { |
| | | //if (i != g_logPtr->fd) |
| | | close(i); |
| | | } |
| | | |
| | | /* Redirect Standard input [0] to /dev/null */ |
| | | fd = open("/dev/null", O_RDWR); |
| | | |
| | | /* Redirect Standard output [1] to /dev/null */ |
| | | dup(fd); |
| | | |
| | | /* Redirect Standard error [2] to /dev/null */ |
| | | dup(fd); |
| | | } |
| | | |
| | | umask(0); |
| | | |
| | | if (!nochdir) |
| | | chdir("/"); |
| | | |
| | | return; |
| | | } |
| | | |
| | | /* **************************************************************************** |
| | | * FunctionName: record_daemon_pid |
| | | * Description : Record the running daemon program PID to the file "pid_file" |
| | | * Inputs : pid_file:The record PID file path |
| | | * Output : NONE |
| | | * Return : 0: Record successfully Else: Failure |
| | | * *****************************************************************************/ |
| | | int record_daemon_pid(const char *pid_file) |
| | | { |
| | | struct stat fStatBuf; |
| | | int fd = -1; |
| | | int mode = S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | S_IRWXU; |
| | | char ipc_dir[64] = { 0 }; |
| | | |
| | | strncpy(ipc_dir, pid_file, 64); |
| | | |
| | | /* dirname() will modify ipc_dir and save the result */ |
| | | dirname(ipc_dir); |
| | | |
| | | /* If folder pid_file PATH doesnot exist, then we will create it" */ |
| | | if (stat(ipc_dir, &fStatBuf) < 0) |
| | | { |
| | | if (mkdir(ipc_dir, mode) < 0) |
| | | { |
| | | log_fatal("cannot create %s: %s\n", ipc_dir, strerror(errno)); |
| | | return -1; |
| | | } |
| | | |
| | | (void)chmod(ipc_dir, mode); |
| | | } |
| | | |
| | | /* Create the process running PID file */ |
| | | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| | | if ((fd = open(pid_file, O_RDWR | O_CREAT | O_TRUNC, mode)) >= 0) |
| | | { |
| | | char pid[PID_ASCII_SIZE]; |
| | | snprintf(pid, sizeof(pid), "%u\n", (unsigned)getpid()); |
| | | write(fd, pid, strlen(pid)); |
| | | close(fd); |
| | | |
| | | log_dbg("Record PID<%u> to file %s.\n", getpid(), pid_file); |
| | | } |
| | | else |
| | | { |
| | | log_fatal("cannot create %s: %s\n", pid_file, strerror(errno)); |
| | | return -1; |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /* **************************************************************************** |
| | | * FunctionName: get_daemon_pid |
| | | * Description : Get the daemon process PID from the PID record file "pid_file" |
| | | * Inputs : pid_file: the PID record file |
| | | * Output : NONE |
| | | * Return : pid_t: The daemon process PID number |
| | | * *****************************************************************************/ |
| | | pid_t get_daemon_pid(const char *pid_file) |
| | | { |
| | | FILE *f; |
| | | pid_t pid; |
| | | |
| | | if ((f = fopen(pid_file, "rb")) != NULL) |
| | | { |
| | | char pid_ascii[PID_ASCII_SIZE]; |
| | | (void)fgets(pid_ascii, PID_ASCII_SIZE, f); |
| | | (void)fclose(f); |
| | | pid = atoi(pid_ascii); |
| | | } |
| | | else |
| | | { |
| | | log_fatal("Can't open PID record file %s: %s\n", pid_file, strerror(errno)); |
| | | return -1; |
| | | } |
| | | return pid; |
| | | } |
| | | |
| | | /* **************************************************************************** |
| | | * FunctionName: check_daemon_running |
| | | * Description : Check the daemon program already running or not |
| | | * Inputs : pid_file: The record running daemon program PID |
| | | * Output : NONE |
| | | * Return : 1: The daemon program alread running 0: Not running |
| | | * *****************************************************************************/ |
| | | int check_daemon_running(const char *pid_file) |
| | | { |
| | | int retVal = -1; |
| | | struct stat fStatBuf; |
| | | |
| | | retVal = stat(pid_file, &fStatBuf); |
| | | if (0 == retVal) |
| | | { |
| | | pid_t pid = -1; |
| | | printf("PID record file \"%s\" exist.\n", pid_file); |
| | | |
| | | pid = get_daemon_pid(pid_file); |
| | | if (pid > 0) /* Process pid exist */ |
| | | { |
| | | if ((retVal = kill(pid, 0)) == 0) |
| | | { |
| | | printf("Program with PID[%d] seems running.\n", pid); |
| | | return 1; |
| | | } |
| | | else /* Send signal to the old process get no reply. */ |
| | | { |
| | | printf("Program with PID[%d] seems exit.\n", pid); |
| | | remove(pid_file); |
| | | return 0; |
| | | } |
| | | } |
| | | else if (0 == pid) |
| | | { |
| | | printf("Can not read program PID form record file.\n"); |
| | | remove(pid_file); |
| | | return 0; |
| | | } |
| | | else /* Read pid from file "pid_file" failure */ |
| | | { |
| | | printf("Read record file \"%s\" failure, maybe program still running.\n", pid_file); |
| | | return 1; |
| | | } |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /* **************************************************************************** |
| | | * FunctionName: stop_daemon_running |
| | | * Description : Stop the daemon program running |
| | | * Inputs : pid_file: The record running daemon program PID |
| | | * Output : NONE |
| | | * Return : 1: The daemon program alread running 0: Not running |
| | | * *****************************************************************************/ |
| | | int stop_daemon_running(const char *pid_file) |
| | | { |
| | | pid_t pid = -1; |
| | | struct stat fStatBuf; |
| | | |
| | | if ( stat(pid_file, &fStatBuf) < 0) |
| | | return 0; |
| | | |
| | | printf("PID record file \"%s\" exist.\n", pid_file); |
| | | pid = get_daemon_pid(pid_file); |
| | | if (pid > 0) /* Process pid exist */ |
| | | { |
| | | while ( (kill(pid, 0) ) == 0) |
| | | { |
| | | kill(pid, SIGTERM); |
| | | sleep(1); |
| | | } |
| | | |
| | | remove(pid_file); |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | |
| | | /* **************************************************************************** |
| | | * FunctionName: set_daemon_running |
| | | * Description : Set the programe running as daemon if it's not running and record |
| | | * its PID to the pid_file. |
| | | * Inputs : pid_file: The record running daemon program PID |
| | | * Output : NONE |
| | | * Return : 0: Successfully. 1: Failure |
| | | * *****************************************************************************/ |
| | | int set_daemon_running(const char *pid_file) |
| | | { |
| | | daemonize(0, 1); |
| | | log_nrml("Program running as daemon [PID:%d].\n", getpid()); |
| | | |
| | | if (record_daemon_pid(pid_file) < 0) |
| | | { |
| | | log_fatal("Record PID to file \"%s\" failure.\n", pid_file); |
| | | return -2; |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | void thread_stop(char *prompt) |
| | | { |
| | | if(prompt) |
| | | log_warn("%s", prompt); |
| | | |
| | | g_signal.threads --; |
| | | pthread_exit(NULL); |
| | | } |
| | | |
| | | int thread_start(pthread_t * thread_id, thread_body worker_func, void *thread_arg) |
| | | { |
| | | int retval = 0; |
| | | |
| | | pthread_attr_t thread_attr; |
| | | |
| | | /* Initialize the thread attribute */ |
| | | retval = pthread_attr_init(&thread_attr); |
| | | if(retval) |
| | | return -1; |
| | | |
| | | /* Set the stack size of the thread */ |
| | | retval = pthread_attr_setstacksize(&thread_attr, 120 * 1024); |
| | | if(retval) |
| | | goto CleanUp; |
| | | |
| | | /* Set thread to detached state:Don`t need pthread_join */ |
| | | retval = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); |
| | | if(retval) |
| | | goto CleanUp; |
| | | |
| | | /* Create the thread */ |
| | | retval = pthread_create(thread_id, &thread_attr, worker_func, thread_arg); |
| | | if(retval) |
| | | goto CleanUp; |
| | | |
| | | |
| | | g_signal.threads++; |
| | | |
| | | CleanUp: |
| | | /* Destroy the attributes of thread */ |
| | | pthread_attr_destroy(&thread_attr); |
| | | return retval; |
| | | } |
| | | |
| | | |
| | | void exec_system_cmd(const char *format, ...) |
| | | { |
| | | char cmd[256]; |
| | | va_list args; |
| | | int done; |
| | | |
| | | memset(cmd, 0, sizeof(cmd)); |
| | | |
| | | va_start(args, format); |
| | | done = vsnprintf(cmd, sizeof(cmd), format, args); |
| | | va_end(args); |
| | | |
| | | system(cmd); |
| | | } |
| | | |
| | | |
New file |
| | |
| | | /******************************************************************************** |
| | | * Copyright: (C) 2012 Guo Wenxue<guowenxue@gmail.com> |
| | | * All rights reserved. |
| | | * |
| | | * Filename: cp_proc.h |
| | | * Description: This head file is for Linux process API |
| | | * |
| | | * Version: 1.0.0(11/06/2012~) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "11/06/2012 09:21:33 PM" |
| | | * |
| | | ********************************************************************************/ |
| | | |
| | | #ifndef _PROC_H_ |
| | | #define _PROC_H_ |
| | | #include <signal.h> |
| | | |
| | | #define PID_ASCII_SIZE 11 |
| | | |
| | | typedef struct proc_signal_s |
| | | { |
| | | int signal; |
| | | unsigned stop; /* 0: Not term 1: Stop */ |
| | | int threads; /* threads counter */ |
| | | } proc_signal_t; |
| | | |
| | | extern proc_signal_t g_signal; |
| | | extern void install_proc_signal(void); |
| | | |
| | | extern void daemonize(int nochdir, int noclose); |
| | | extern int record_daemon_pid(const char *pid_file); |
| | | extern pid_t get_daemon_pid(const char *pid_file); |
| | | extern int check_daemon_running(const char *pid_file); |
| | | extern int stop_daemon_running(const char *pid_file); |
| | | extern int set_daemon_running(const char *pid_file); |
| | | |
| | | extern void exec_system_cmd(const char *format, ...); |
| | | |
| | | typedef void *(* thread_body)(void *thread_arg); |
| | | extern int thread_start(pthread_t * thread_id, thread_body worker_func, void *thread_arg); |
| | | |
| | | extern void thread_stop(char *prompt); |
| | | |
| | | #endif /* ----- #ifndef _PROC_H_ ----- */ |
New file |
| | |
| | | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ |
| | | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ |
| | | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ |
| | | !_TAG_PROGRAM_NAME Exuberant Ctags // |
| | | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ |
| | | !_TAG_PROGRAM_VERSION 5.9~svn20110310 // |
| | | BUFSIZE tlv_sample.c 21;" d file: |
| | | CHARS_PER_LINE logger.c 334;" d file: |
| | | CHARS_PER_LINE tlv_sample.c 156;" d file: |
| | | CRC_ITU_T_H crc-itu-t.h 14;" d |
| | | DBG_LOG_FILE logger.h 35;" d |
| | | DEFAULT_TIME_FORMAT logger.h 40;" d |
| | | FILENAME_LEN logger.h 32;" d |
| | | FLAG_LOGGER_CONSOLE logger.h 63;" d |
| | | FLAG_LOGGER_FILE logger.h 64;" d |
| | | FLAG_LOGGER_LEVEL_OPT logger.h 61;" d |
| | | LINELEN logger.c 333;" d file: |
| | | LINELEN tlv_sample.c 155;" d file: |
| | | LOG_FILE_LINE logger.h 92;" d |
| | | LOG_LEVEL_DEBUG logger.h /^ LOG_LEVEL_DEBUG, \/* Debug Level "Debug" *\/$/;" e enum:__anon1 |
| | | LOG_LEVEL_DISB logger.h /^ LOG_LEVEL_DISB = 0, \/* Disable "Debug" *\/$/;" e enum:__anon1 |
| | | LOG_LEVEL_ERROR logger.h /^ LOG_LEVEL_ERROR, \/* Debug Level "ERROR" *\/$/;" e enum:__anon1 |
| | | LOG_LEVEL_FATAL logger.h /^ LOG_LEVEL_FATAL, \/* Debug Level "Fatal" *\/$/;" e enum:__anon1 |
| | | LOG_LEVEL_INFO logger.h /^ LOG_LEVEL_INFO, \/* Debug Level "Information" *\/$/;" e enum:__anon1 |
| | | LOG_LEVEL_MAX logger.h /^ LOG_LEVEL_MAX,$/;" e enum:__anon1 |
| | | LOG_LEVEL_NRML logger.h /^ LOG_LEVEL_NRML, \/* Debug Level "Normal" *\/$/;" e enum:__anon1 |
| | | LOG_LEVEL_TRACE logger.h /^ LOG_LEVEL_TRACE, \/* Debug Level "Trace" *\/$/;" e enum:__anon1 |
| | | LOG_LEVEL_WARN logger.h /^ LOG_LEVEL_WARN, \/* Debug Level "warnning" *\/$/;" e enum:__anon1 |
| | | LOG_ROLLBACK_NONE logger.h 38;" d |
| | | LOG_ROLLBACK_SIZE logger.h 37;" d |
| | | LOG_TIME_FMT logger.c 24;" d file: |
| | | LOG_VERSION_STR logger.h 29;" d |
| | | MAGIC_CRC crc-itu-t.h 16;" d |
| | | MAX_LOG_MESSAGE_LEN logger.h 41;" d |
| | | OFF tlv_sample.c 18;" d file: |
| | | ON tlv_sample.c 19;" d file: |
| | | PACK_HEADER tlv_sample.c 30;" d file: |
| | | PID_ASCII_SIZE proc.h 18;" d |
| | | PRECISE_TIME_FACTOR logger.c 16;" d file: |
| | | TAG_CAMERA tlv_sample.c /^ TAG_CAMERA,$/;" e enum:__anon2 file: |
| | | TAG_LED tlv_sample.c /^ TAG_LED,$/;" e enum:__anon2 file: |
| | | TAG_LOGON tlv_sample.c /^ TAG_LOGON=1,$/;" e enum:__anon2 file: |
| | | TLV_FIXED_SIZE tlv_sample.c 41;" d file: |
| | | TLV_MIN_SIZE tlv_sample.c 44;" d file: |
| | | _LOGGER_H_ logger.h 15;" d |
| | | _PROC_H_ proc.h 15;" d |
| | | _curOffset logger.c /^ long _curOffset = ftell(logger->fp);$/;" l |
| | | act logger.c /^ struct sigaction act;$/;" l |
| | | argp logger.c /^ va_list argp;$/;" l |
| | | args proc.c /^ va_list args;$/;" l |
| | | buf logger.c /^ char buf[MAX_LOG_MESSAGE_LEN];$/;" l |
| | | buf tlv_sample.c /^ char buf[BUFSIZE];$/;" l |
| | | bytes tlv_sample.c /^ int bytes;$/;" l |
| | | bytes_to_ushort crc-itu-t.c /^unsigned short bytes_to_ushort(unsigned char *bytes, int len)$/;" f signature:(unsigned char *bytes, int len) |
| | | bytes_to_ushort crc-itu-t.h /^extern unsigned short bytes_to_ushort(unsigned char *bytes, int len);$/;" p signature:(unsigned char *bytes, int len) |
| | | c logger.c /^ unsigned char c = buf[idx];$/;" l |
| | | c tlv_sample.c /^ unsigned char c = data[idx];$/;" l |
| | | check_and_rollback logger.c /^static void check_and_rollback(void)$/;" f file: signature:(void) |
| | | check_daemon_running proc.c /^int check_daemon_running(const char *pid_file)$/;" f signature:(const char *pid_file) |
| | | check_daemon_running proc.h /^extern int check_daemon_running(const char *pid_file);$/;" p signature:(const char *pid_file) |
| | | chmod proc.c /^ (void)chmod(ipc_dir, mode); $/;" p file: |
| | | cmd logger.c /^ char cmd[512];$/;" l |
| | | cmd proc.c /^ char cmd[256];$/;" l |
| | | cp_install_proc_signal proc.c /^void cp_install_proc_signal(void)$/;" f signature:(void) |
| | | cp_install_proc_signal proc.h /^extern void cp_install_proc_signal(void);$/;" p signature:(void) |
| | | cp_printout logger.c /^static void cp_printout(char *level, char *fmt, va_list argp)$/;" f file: signature:(char *level, char *fmt, va_list argp) |
| | | cp_printout_line logger.c /^static void cp_printout_line(char *level, char *fmt, char *file, int line, va_list argp)$/;" f file: signature:(char *level, char *fmt, char *file, int line, va_list argp) |
| | | cp_proc_sighandler proc.c /^void cp_proc_sighandler(int sig)$/;" f signature:(int sig) |
| | | crc16 tlv_sample.c /^ unsigned short crc16 = 0;$/;" l |
| | | crc16 tlv_sample.c /^ unsigned short crc16;$/;" l |
| | | crc_itu_t crc-itu-t.c /^unsigned short crc_itu_t(unsigned short crc, const unsigned char *buffer, unsigned int len)$/;" f signature:(unsigned short crc, const unsigned char *buffer, unsigned int len) |
| | | crc_itu_t crc-itu-t.h /^extern unsigned short crc_itu_t(unsigned short magic_crc, const unsigned char *buffer, unsigned int len);$/;" p signature:(unsigned short magic_crc, const unsigned char *buffer, unsigned int len) |
| | | crc_itu_t_byte crc-itu-t.h /^static inline unsigned short crc_itu_t_byte(unsigned short crc, const unsigned char data)$/;" f signature:(unsigned short crc, const unsigned char data) |
| | | crc_itu_t_table crc-itu-t.c /^const unsigned short crc_itu_t_table[256] = {$/;" v |
| | | crc_itu_t_table crc-itu-t.h /^extern const unsigned short crc_itu_t_table[256];$/;" x |
| | | daemonize proc.c /^void daemonize(int nochdir, int noclose)$/;" f signature:(int nochdir, int noclose) |
| | | daemonize proc.h /^extern void daemonize(int nochdir, int noclose);$/;" p signature:(int nochdir, int noclose) |
| | | data_len tlv_sample.c /^ int data_len = 0;$/;" l |
| | | done proc.c /^ int done; $/;" l |
| | | dump_buf tlv_sample.c /^void dump_buf(char *data, int len)$/;" f signature:(char *data, int len) |
| | | dump_buf tlv_sample.c /^void dump_buf(char *data, int len);$/;" p file: signature:(char *data, int len) |
| | | exec_system_cmd proc.c /^void exec_system_cmd(const char *format, ...)$/;" f signature:(const char *format, ...) |
| | | exec_system_cmd proc.h /^extern void exec_system_cmd(const char *format, ...);$/;" p signature:(const char *format, ...) |
| | | f proc.c /^ FILE *f; $/;" l |
| | | fStatBuf proc.c /^ struct stat fStatBuf;$/;" l |
| | | fStatBuf proc.c /^ struct stat fStatBuf; $/;" l |
| | | fStatBuf proc.c /^ struct stat fStatBuf;$/;" l |
| | | fclose proc.c /^ (void)fclose(f); $/;" p file: |
| | | fd proc.c /^ int fd = -1; $/;" l |
| | | fd proc.c /^ int retval, fd; $/;" l |
| | | fgets proc.c /^ (void)fgets(pid_ascii, PID_ASCII_SIZE, f); $/;" p file: |
| | | file logger.h /^ char file[FILENAME_LEN];$/;" m struct:logger_s access:public |
| | | filemode logger.c /^ char *filemode;$/;" l |
| | | flag logger.h /^ unsigned char flag;$/;" m struct:logger_s access:public |
| | | fp logger.h /^ FILE *fp;$/;" m struct:logger_s access:public |
| | | g_signal proc.c /^proc_signal_t g_signal={0};$/;" v |
| | | g_signal proc.h /^extern proc_signal_t g_signal;$/;" x |
| | | get_daemon_pid proc.c /^pid_t get_daemon_pid(const char *pid_file)$/;" f signature:(const char *pid_file) |
| | | get_daemon_pid proc.h /^extern pid_t get_daemon_pid(const char *pid_file);$/;" p signature:(const char *pid_file) |
| | | hc logger.c /^ char hc[4];$/;" l |
| | | hc tlv_sample.c /^ char hc[4];$/;" l |
| | | i crc-itu-t.c /^ int i = 0;$/;" l |
| | | i crc-itu-t.c /^ int i = 0;$/;" l |
| | | i proc.c /^ int i; $/;" l |
| | | idx logger.c /^ int idx;$/;" l |
| | | idx tlv_sample.c /^ int idx;$/;" l |
| | | ipc_dir proc.c /^ char ipc_dir[64] = { 0 }; $/;" l |
| | | ldx logger.c /^ int ldx = idx % CHARS_PER_LINE;$/;" l |
| | | level logger.h /^ int level;$/;" m struct:logger_s access:public |
| | | line_done logger.c /^ short line_done = 1;$/;" l |
| | | line_done tlv_sample.c /^ short line_done = 1;$/;" l |
| | | lit logger.c /^ char lit[CHARS_PER_LINE + 2];$/;" l |
| | | lit tlv_sample.c /^ char lit[CHARS_PER_LINE + 1];$/;" l |
| | | local logger.c /^ struct tm *local;$/;" l |
| | | log_dbg logger.h 105;" d |
| | | log_dbg logger.h 97;" d |
| | | log_err logger.h 100;" d |
| | | log_err logger.h 108;" d |
| | | log_fatal logger.h 101;" d |
| | | log_fatal logger.h 109;" d |
| | | log_info logger.h 104;" d |
| | | log_info logger.h 96;" d |
| | | log_nrml logger.h 106;" d |
| | | log_nrml logger.h 98;" d |
| | | log_rollback_size logger.c /^static unsigned long log_rollback_size = LOG_ROLLBACK_NONE;$/;" v file: |
| | | log_signal_handler logger.c /^static void log_signal_handler(int sig)$/;" f file: signature:(int sig) |
| | | log_str logger.c /^char *log_str[LOG_LEVEL_MAX + 1] = { "", "F", "E", "W", "N", "D", "I", "T", "M" };$/;" v |
| | | log_str logger.h /^extern char *log_str[];$/;" x |
| | | log_trace logger.h 103;" d |
| | | log_trace logger.h 95;" d |
| | | log_warn logger.h 107;" d |
| | | log_warn logger.h 99;" d |
| | | logger logger.c /^static logger_t *logger = NULL;$/;" v file: |
| | | logger_banner logger.c /^static void logger_banner(char *prefix)$/;" f file: signature:(char *prefix) |
| | | logger_close logger.c /^void logger_close(void)$/;" f signature:(void) |
| | | logger_close logger.h /^extern void logger_close(void);$/;" p signature:(void) |
| | | logger_dump logger.c /^void logger_dump(int level, char *buf, int len)$/;" f signature:(int level, char *buf, int len) |
| | | logger_dump logger.h /^extern void logger_dump(int level, char *buf, int len);$/;" p signature:(int level, char *buf, int len) |
| | | logger_init logger.c /^int logger_init(logger_t *log, char *log_file, int level, int log_size)$/;" f signature:(logger_t *log, char *log_file, int level, int log_size) |
| | | logger_init logger.h /^extern int logger_init(logger_t *logger, char *filename, int level, int log_size);$/;" p signature:(logger_t *logger, char *filename, int level, int log_size) |
| | | logger_line logger.c /^void logger_line(int level, char *file, int line, char *fmt, ...)$/;" f signature:(int level, char *file, int line, char *fmt, ...) |
| | | logger_line logger.h /^extern void logger_line(int level, char *file, int line, char *fmt, ...);$/;" p signature:(int level, char *file, int line, char *fmt, ...) |
| | | logger_min logger.c /^void logger_min(int level, char *fmt, ...)$/;" f signature:(int level, char *fmt, ...) |
| | | logger_min logger.h /^extern void logger_min(int level, char *fmt, ...);$/;" p signature:(int level, char *fmt, ...) |
| | | logger_open logger.c /^int logger_open(void)$/;" f signature:(void) |
| | | logger_open logger.h /^extern int logger_open(void);$/;" p signature:(void) |
| | | logger_raw logger.c /^void logger_raw(const char *fmt, ...)$/;" f signature:(const char *fmt, ...) |
| | | logger_raw logger.h /^extern void logger_raw(const char *fmt, ...);$/;" p signature:(const char *fmt, ...) |
| | | logger_reopen logger.c /^int logger_reopen(void)$/;" f signature:(void) |
| | | logger_reopen logger.h /^extern int logger_reopen(void);$/;" p signature:(void) |
| | | logger_s logger.h /^typedef struct logger_s$/;" s |
| | | logger_s::file logger.h /^ char file[FILENAME_LEN];$/;" m struct:logger_s access:public |
| | | logger_s::flag logger.h /^ unsigned char flag;$/;" m struct:logger_s access:public |
| | | logger_s::fp logger.h /^ FILE *fp;$/;" m struct:logger_s access:public |
| | | logger_s::level logger.h /^ int level;$/;" m struct:logger_s access:public |
| | | logger_s::size logger.h /^ int size;$/;" m struct:logger_s access:public |
| | | logger_set_time_format logger.h /^extern void logger_set_time_format(char *time_format);$/;" p signature:(char *time_format) |
| | | logger_str logger.c /^void logger_str(int level, const char *msg)$/;" f signature:(int level, const char *msg) |
| | | logger_str logger.h /^extern void logger_str(int level, const char *msg);$/;" p signature:(int level, const char *msg) |
| | | logger_t logger.h /^} logger_t;$/;" t typeref:struct:logger_s |
| | | logger_term logger.c /^void logger_term(void)$/;" f signature:(void) |
| | | logger_term logger.h /^extern void logger_term(void);$/;" p signature:(void) |
| | | main tlv_sample.c /^int main(int argc, char **argv)$/;" f signature:(int argc, char **argv) |
| | | mode proc.c /^ int mode = S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | S_IRWXU;$/;" l |
| | | now logger.c /^ struct timeval now;$/;" l |
| | | ofset tlv_sample.c /^ int ofset = 0; \/* index position for the buf *\/$/;" l |
| | | pack_len tlv_sample.c /^ int pack_len = 0;$/;" l |
| | | pack_len tlv_sample.c /^ int pack_len = TLV_FIXED_SIZE+1; \/* Only 1 byte data *\/$/;" l |
| | | packtlv_led tlv_sample.c /^int packtlv_led(char *buf, int size, int cmd)$/;" f signature:(char *buf, int size, int cmd) |
| | | packtlv_led tlv_sample.c /^int packtlv_led(char *buf, int size, int cmd);$/;" p file: signature:(char *buf, int size, int cmd) |
| | | packtlv_logon tlv_sample.c /^int packtlv_logon(char *buf, int size, char *pwd)$/;" f signature:(char *buf, int size, char *pwd) |
| | | packtlv_logon tlv_sample.c /^int packtlv_logon(char *buf, int size, char *pwd);$/;" p file: signature:(char *buf, int size, char *pwd) |
| | | pid proc.c /^ char pid[PID_ASCII_SIZE]; $/;" l |
| | | pid proc.c /^ pid_t pid = -1; $/;" l |
| | | pid proc.c /^ pid_t pid = -1; $/;" l |
| | | pid proc.c /^ pid_t pid; $/;" l |
| | | pid_ascii proc.c /^ char pid_ascii[PID_ASCII_SIZE]; $/;" l |
| | | print_char logger.c /^static char *print_char =$/;" v file: |
| | | print_char tlv_sample.c /^static char *print_char =$/;" v file: |
| | | prn logger.c /^ char prn[LINELEN];$/;" l |
| | | prn tlv_sample.c /^ char prn[LINELEN];$/;" l |
| | | proc_signal_s proc.h /^typedef struct proc_signal_s$/;" s |
| | | proc_signal_s::signal proc.h /^ int signal;$/;" m struct:proc_signal_s access:public |
| | | proc_signal_s::stop proc.h /^ unsigned stop; \/* 0: Not term 1: Stop *\/$/;" m struct:proc_signal_s access:public |
| | | proc_signal_s::threads proc.h /^ int threads; \/* threads counter *\/$/;" m struct:proc_signal_s access:public |
| | | proc_signal_t proc.h /^} proc_signal_t;$/;" t typeref:struct:proc_signal_s |
| | | rc logger.c /^ int rc = 0;$/;" l |
| | | rc logger.c /^ int rc;$/;" l |
| | | rc tlv_sample.c /^ int rc;$/;" l |
| | | record_daemon_pid proc.c /^int record_daemon_pid(const char *pid_file)$/;" f signature:(const char *pid_file) |
| | | record_daemon_pid proc.h /^extern int record_daemon_pid(const char *pid_file);$/;" p signature:(const char *pid_file) |
| | | retVal proc.c /^ int retVal = -1; $/;" l |
| | | retval proc.c /^ int retval = 0;$/;" l |
| | | retval proc.c /^ int retval, fd; $/;" l |
| | | set_daemon_running proc.c /^int set_daemon_running(const char *pid_file)$/;" f signature:(const char *pid_file) |
| | | set_daemon_running proc.h /^extern int set_daemon_running(const char *pid_file);$/;" p signature:(const char *pid_file) |
| | | sigact proc.c /^ struct sigaction sigact, sigign;$/;" l |
| | | sigign proc.c /^ struct sigaction sigact, sigign;$/;" l |
| | | signal proc.h /^ int signal;$/;" m struct:proc_signal_s access:public |
| | | size crc-itu-t.c /^ int size = sizeof (unsigned short);$/;" l |
| | | size logger.h /^ int size;$/;" m struct:logger_s access:public |
| | | stop proc.h /^ unsigned stop; \/* 0: Not term 1: Stop *\/$/;" m struct:proc_signal_s access:public |
| | | stop_daemon_running proc.c /^int stop_daemon_running(const char *pid_file)$/;" f signature:(const char *pid_file) |
| | | stop_daemon_running proc.h /^extern int stop_daemon_running(const char *pid_file);$/;" p signature:(const char *pid_file) |
| | | thread_attr proc.c /^ pthread_attr_t thread_attr; $/;" l |
| | | thread_body proc.h /^typedef void *(* thread_body) (void *thread_arg);$/;" t |
| | | thread_start proc.c /^int thread_start(pthread_t * thread_id, thread_body worker_func, void *thread_arg)$/;" f signature:(pthread_t * thread_id, thread_body worker_func, void *thread_arg) |
| | | thread_start proc.h /^extern int thread_start(pthread_t * thread_id, thread_body worker_func, void *thread_arg);$/;" p signature:(pthread_t * thread_id, thread_body worker_func, void *thread_arg) |
| | | thread_stop proc.c /^void thread_stop(char *prompt)$/;" f signature:(char *prompt) |
| | | thread_stop proc.h /^extern void thread_stop(char *prompt);$/;" p signature:(char *prompt) |
| | | threads proc.h /^ int threads; \/* threads counter *\/$/;" m struct:proc_signal_s access:public |
| | | timestr logger.c /^ char timestr[256];$/;" l |
| | | ushort_to_bytes crc-itu-t.c /^int ushort_to_bytes(unsigned char *bytes, unsigned short val)$/;" f signature:(unsigned char *bytes, unsigned short val) |
| | | ushort_to_bytes crc-itu-t.h /^extern int ushort_to_bytes(unsigned char *bytes, unsigned short val);$/;" p signature:(unsigned char *bytes, unsigned short val) |
| | | val crc-itu-t.c /^ unsigned short val = 0;$/;" l |
New file |
| | |
| | | /********************************************************************************* |
| | | * Copyright: (C) 2019 LingYun IoT studio |
| | | * All rights reserved. |
| | | * |
| | | * Filename: tlv_sample.c |
| | | * Description: This file |
| | | * |
| | | * Version: 1.0.0(03/27/2019) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "03/27/2019 09:20:25 PM" |
| | | * |
| | | ********************************************************************************/ |
| | | |
| | | #include <stdio.h> |
| | | #include <string.h> |
| | | #include "crc-itu-t.h" |
| | | |
| | | #define OFF 0 |
| | | #define ON 1 |
| | | |
| | | #define BUFSIZE 128 |
| | | |
| | | /* TLV Packet format: |
| | | * |
| | | *+------------+---------+------------+-----------+-----------+ |
| | | *| Header(1B) | Tag(1B) | Length(1B) | Value(1B) | CRC16(2B) | |
| | | *+------------+---------+------------+-----------+-----------+ |
| | | */ |
| | | |
| | | #define PACK_HEADER 0xFD |
| | | |
| | | /* Tag definition */ |
| | | enum |
| | | { |
| | | TAG_LOGON=1, |
| | | TAG_CAMERA, |
| | | TAG_LED, |
| | | }; |
| | | |
| | | /* TLV packet fixed segement size, 1B Head, 1B Tag, 1B length, 2B CRC16, total 5B. */ |
| | | #define TLV_FIXED_SIZE 5 |
| | | |
| | | /* TLV packet Minimum size is fixed bytes + 1 byte data */ |
| | | #define TLV_MIN_SIZE (TLV_FIXED_SIZE+1) |
| | | |
| | | int packtlv_logon(char *buf, int size, char *pwd); |
| | | int packtlv_led(char *buf, int size, int cmd); |
| | | void dump_buf(char *data, int len); |
| | | |
| | | int main(int argc, char **argv) |
| | | { |
| | | char buf[BUFSIZE]; |
| | | int bytes; |
| | | |
| | | bytes = packtlv_led(buf, sizeof(buf), ON); |
| | | |
| | | /* print every byte in the buffer as HEX and corresponding charactor, |
| | | which is not printable charactor will be instead as '?' */ |
| | | dump_buf(buf, bytes); |
| | | |
| | | bytes = packtlv_logon(buf, sizeof(buf), "iot@yun"); |
| | | |
| | | dump_buf(buf, bytes); |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | int packtlv_logon(char *buf, int size, char *pwd) |
| | | { |
| | | unsigned short crc16 = 0; |
| | | int pack_len = 0; |
| | | int data_len = 0; |
| | | int ofset = 0; /* index position for the buf */ |
| | | |
| | | if(!buf || !pwd || size<TLV_MIN_SIZE ) |
| | | { |
| | | printf("Invalid input arguments\n"); |
| | | return 0; |
| | | } |
| | | |
| | | /* Packet head */ |
| | | buf[ofset] = PACK_HEADER; |
| | | ofset += 1; |
| | | |
| | | /* Tag */ |
| | | buf[ofset] = TAG_LOGON; |
| | | ofset += 1; |
| | | |
| | | /* $pwd too long maybe result buffer overflow, so we need check the buffer |
| | | * is large enuf or not. If buf size is not enuf we will truncate $pwd string */ |
| | | if( strlen(pwd) <= size-TLV_FIXED_SIZE ) |
| | | data_len = strlen(pwd); |
| | | else |
| | | data_len = size-TLV_FIXED_SIZE; |
| | | |
| | | /* Length, this packet is passwd length+5Byte () */ |
| | | pack_len = data_len + TLV_FIXED_SIZE; |
| | | |
| | | buf[ofset] = pack_len; |
| | | ofset += 1; |
| | | |
| | | memcpy(&buf[ofset], pwd, data_len); |
| | | ofset += data_len; |
| | | |
| | | /* Calc CRC16 checksum value from Packet Head(buf[0])~ Value(buf[ofset]) */ |
| | | crc16 = crc_itu_t(MAGIC_CRC, buf, ofset); |
| | | |
| | | /* Append the 2 Bytes CRC16 checksum value into the last two bytes in packet buffer */ |
| | | ushort_to_bytes(&buf[ofset], crc16); |
| | | ofset += 2; |
| | | |
| | | return ofset; |
| | | } |
| | | |
| | | |
| | | |
| | | int packtlv_led(char *buf, int size, int cmd) |
| | | { |
| | | unsigned short crc16; |
| | | int pack_len = TLV_FIXED_SIZE+1; /* Only 1 byte data */ |
| | | |
| | | if(!buf || size<TLV_MIN_SIZE ) |
| | | { |
| | | printf("Invalid input arguments\n"); |
| | | return 0; |
| | | } |
| | | |
| | | /* Packet head */ |
| | | buf[0] = PACK_HEADER; |
| | | |
| | | /* Tag */ |
| | | buf[1] = TAG_LED; |
| | | |
| | | /* Length, this packet total 6 bytes */ |
| | | buf[2] = pack_len; |
| | | |
| | | /* Value */ |
| | | buf[3] = (OFF==cmd) ? 0x00 : 0x01; |
| | | |
| | | /* Calc CRC16 checksum value from Packet Head(buf[0])~ Packet Value(buf[3]) */ |
| | | crc16 = crc_itu_t(MAGIC_CRC, buf, 4); |
| | | |
| | | /* Append the 2 Bytes CRC16 checksum value into the last two bytes in packet buffer */ |
| | | ushort_to_bytes(&buf[4], crc16); |
| | | |
| | | return pack_len; |
| | | } |
| | | |
| | | |
| | | /*+------------------------------+ |
| | | *| dump_buf() implement code | |
| | | *+------------------------------+*/ |
| | | |
| | | #define LINELEN 81 |
| | | #define CHARS_PER_LINE 16 |
| | | |
| | | static char *print_char = |
| | | " " |
| | | " " |
| | | " !\"#$%&'()*+,-./" |
| | | "0123456789:;<=>?" |
| | | "@ABCDEFGHIJKLMNO" |
| | | "PQRSTUVWXYZ[\\]^_" |
| | | "`abcdefghijklmno" |
| | | "pqrstuvwxyz{|}~ " |
| | | " " |
| | | " " |
| | | " ???????????????" |
| | | "????????????????" |
| | | "????????????????" |
| | | "????????????????" |
| | | "????????????????" |
| | | "????????????????"; |
| | | |
| | | /* print every byte in the buffer as HEX and corresponding charactor, which is not printable charactor will be instead as '?' */ |
| | | void dump_buf(char *data, int len) |
| | | { |
| | | int rc; |
| | | int idx; |
| | | char prn[LINELEN]; |
| | | char lit[CHARS_PER_LINE + 1]; |
| | | char hc[4]; |
| | | short line_done = 1; |
| | | |
| | | rc = len; |
| | | idx = 0; |
| | | lit[CHARS_PER_LINE] = '\0'; |
| | | |
| | | while (rc > 0) |
| | | { |
| | | if (line_done) |
| | | snprintf(prn, LINELEN, "%08X: ", idx); |
| | | do |
| | | { |
| | | unsigned char c = data[idx]; |
| | | snprintf(hc, 4, "%02X ", c); |
| | | strncat(prn, hc, 4); |
| | | lit[idx % CHARS_PER_LINE] = print_char[c]; |
| | | ++idx; |
| | | } while (--rc > 0 && (idx % CHARS_PER_LINE != 0)); |
| | | |
| | | line_done = (idx % CHARS_PER_LINE) == 0; |
| | | |
| | | if (line_done) |
| | | printf("%s %s\n", prn, lit); |
| | | else if (rc == 0) |
| | | strncat(prn, " ", LINELEN); |
| | | } |
| | | |
| | | if (!line_done) |
| | | { |
| | | lit[(idx % CHARS_PER_LINE)] = '\0'; |
| | | |
| | | while ((++idx % CHARS_PER_LINE) != 0) |
| | | strncat(prn, " ", LINELEN); |
| | | |
| | | printf("%s %s\n", prn, lit); |
| | | |
| | | } |
| | | } |
| | | |
New file |
| | |
| | | |
| | | APP1_NAME=tlv_client |
| | | APP2_NAME=tlv_server |
| | | |
| | | #CROSSTOOL?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux- |
| | | CC=${CROSSTOOL}gcc |
| | | LD=${CROSSTOOL}ld |
| | | AS=${CROSSTOOL}as |
| | | AR=${CROSSTOOL}ar |
| | | |
| | | # subdirectory |
| | | SUBDIR1=lylib |
| | | |
| | | # Compile flags |
| | | CFLAGS=-Wall -Werror |
| | | CFLAGS+=-I${SUBDIR1} |
| | | |
| | | # Linker flags |
| | | LDFLAGS=-L${SUBDIR1} -l${SUBDIR1} |
| | | LDFLAGS+=-lpthread |
| | | |
| | | |
| | | all: banner lib${SUBDIR1} |
| | | ${CC} ${CFLAGS} tlv_client.c -o ${APP1_NAME} ${LDFLAGS} |
| | | ${CC} ${CFLAGS} tlv_server.c -o ${APP2_NAME} ${LDFLAGS} |
| | | |
| | | banner: |
| | | @echo "Start to compile ${SRC} by ${CC}" |
| | | |
| | | lib${SUBDIR1}: |
| | | @make CROSSTOOL=${CROSSTOOL} -C ${SUBDIR1} |
| | | |
| | | clean: |
| | | @make clean -C ${SUBDIR1} |
| | | rm -f ${APP1_NAME} ${APP2_NAME} |
| | | |
| | | distclean: clean |
| | | @make distclean -C ${SUBDIR1} |
| | | @rm -f *.o |
| | | @rm -f cscope* tags |
| | | @rm -f *.log |
| | | |
New file |
| | |
| | | /********************************************************************************* |
| | | * Copyright: (C) 2020 LingYun IoT System Studio |
| | | * All rights reserved. |
| | | * |
| | | * Filename: tlv_client.c |
| | | * Description: This is a socket client program used to report RPi's temperature |
| | | * to socket server by TLV protocal. |
| | | * |
| | | * Version: 1.0.0(2020年04月14日) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "2020年04月14日 00时52分56秒" |
| | | * |
| | | ********************************************************************************/ |
| | | #include <stdio.h> |
| | | #include <unistd.h> |
| | | #include <string.h> |
| | | #include <getopt.h> |
| | | #include <libgen.h> |
| | | |
| | | #include "logger.h" |
| | | #include "proc.h" |
| | | |
| | | #define PROG_VERSION "1.0.0" |
| | | |
| | | #define DEF_LOG_FILE "tlv_client.log" |
| | | |
| | | static void banner(void) |
| | | { |
| | | printf("Version %s build on %s %s\n", PROG_VERSION, __DATE__, __TIME__); |
| | | printf("Copyright (C) 2020 LingYun IoT System Studio.\n\n"); |
| | | |
| | | return ; |
| | | } |
| | | |
| | | static void program_usage(const char *progname) |
| | | { |
| | | printf("Usage: %s [OPTION]...\n", progname); |
| | | printf("This program used to connect to server and send RPi's temperature by TLV protocal.\n"); |
| | | |
| | | printf("\nMandatory arguments to long options are mandatory for short options too:\n"); |
| | | printf(" -d[debug ] Running in debug mode\n"); |
| | | printf(" -l[level ] Set the log level as [0..%d]\n", LOG_LEVEL_MAX-1); |
| | | printf(" -h[help ] Display this help information\n"); |
| | | printf(" -v[version ] Display the program version\n"); |
| | | printf("\n"); |
| | | |
| | | banner(); |
| | | return ; |
| | | } |
| | | |
| | | int main (int argc, char *argv[]) |
| | | { |
| | | int opt; |
| | | int i = 0; |
| | | //int rv = 0; |
| | | int debug = 0; |
| | | |
| | | char pid_file[64] = { 0 }; /* The file used to record the PID */ |
| | | const char *progname=NULL; |
| | | |
| | | int log_level = LOG_LEVEL_NRML; |
| | | char *log_file = DEF_LOG_FILE; |
| | | logger_t logger; |
| | | |
| | | struct option long_options[] = { |
| | | {"debug", no_argument, NULL, 'd'}, |
| | | {"level", required_argument, NULL, 'l'}, |
| | | {"version", no_argument, NULL, 'v'}, |
| | | {"help", no_argument, NULL, 'h'}, |
| | | {NULL, 0, NULL, 0} |
| | | }; |
| | | |
| | | memset(&logger, 0, sizeof(logger)); |
| | | |
| | | progname = basename(argv[0]); /* get program name */ |
| | | |
| | | /* parser the command line parameters */ |
| | | while ((opt = getopt_long(argc, argv, "c:dl:vh", long_options, NULL)) != -1) |
| | | { |
| | | switch (opt) |
| | | { |
| | | |
| | | case 'd': /* set debug running */ |
| | | debug = 1; |
| | | log_level= LOG_LEVEL_DEBUG; |
| | | log_file = NULL; /* use standard output */ |
| | | break; |
| | | |
| | | case 'l': /* set the log level */ |
| | | i = atoi(optarg); |
| | | log_level = i>LOG_LEVEL_MAX ? LOG_LEVEL_MAX-1 : i; |
| | | logger.flag |= FLAG_LOGGER_LEVEL_OPT; |
| | | break; |
| | | |
| | | case 'v': /* print software version */ |
| | | banner(); |
| | | return EXIT_SUCCESS; |
| | | |
| | | case 'h': /* print help information */ |
| | | program_usage(progname); |
| | | return 0; |
| | | |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | /* check program already running or not, if already running then exit, or set running as daemon */ |
| | | snprintf(pid_file, sizeof(pid_file), "/var/run/%s.pid", progname); |
| | | if( !debug ) |
| | | { |
| | | if( check_daemon_running(pid_file) ) |
| | | { |
| | | printf("Programe already running, exit now.\n"); |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | /* initial and open logger system */ |
| | | if( logger_init(&logger, log_file, log_level, 512)<0 || logger_open()<0 ) |
| | | { |
| | | printf("ERROR: Initialise logger system failure\n"); |
| | | return -1; |
| | | } |
| | | |
| | | /* install signal proc handler */ |
| | | install_proc_signal(); |
| | | |
| | | log_nrml("Program start running\n"); |
| | | |
| | | /* g_signal.stop defined in proc.c, and will be set when catch stop signal */ |
| | | while( !g_signal.stop ) |
| | | { |
| | | log_dbg("Program still running\n"); |
| | | sleep(3); |
| | | } |
| | | |
| | | logger_term(); |
| | | |
| | | return 0; |
| | | } |
| | | |
New file |
| | |
| | | /********************************************************************************* |
| | | * Copyright: (C) 2020 LingYun IoT System Studio |
| | | * All rights reserved. |
| | | * |
| | | * Filename: tlv_server.c |
| | | * Description: This is a socket server program to receive RPi's temperature |
| | | * from socket client and save it in sqlite database. |
| | | * |
| | | * Version: 1.0.0(2020年04月14日) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "2020年04月14日 00时52分56秒" |
| | | * |
| | | ********************************************************************************/ |
| | | #include <stdio.h> |
| | | #include <unistd.h> |
| | | #include <string.h> |
| | | #include <getopt.h> |
| | | #include <libgen.h> |
| | | |
| | | #include "logger.h" |
| | | #include "proc.h" |
| | | |
| | | #define PROG_VERSION "1.0.0" |
| | | |
| | | #define DEF_LOG_FILE "tlv_server.log" |
| | | |
| | | static void banner(void) |
| | | { |
| | | printf("Version %s build on %s %s\n", PROG_VERSION, __DATE__, __TIME__); |
| | | printf("Copyright (C) 2020 LingYun IoT System Studio.\n\n"); |
| | | |
| | | return ; |
| | | } |
| | | |
| | | static void program_usage(const char *progname) |
| | | { |
| | | printf("Usage: %s [OPTION]...\n", progname); |
| | | printf("This program receive RPi's temperature from socket client and save it in sqlite database.\n"); |
| | | |
| | | printf("\nMandatory arguments to long options are mandatory for short options too:\n"); |
| | | printf(" -d[debug ] Running in debug mode\n"); |
| | | printf(" -l[level ] Set the log level as [0..%d]\n", LOG_LEVEL_MAX-1); |
| | | printf(" -h[help ] Display this help information\n"); |
| | | printf(" -v[version ] Display the program version\n"); |
| | | printf("\n"); |
| | | |
| | | banner(); |
| | | return ; |
| | | } |
| | | |
| | | int main (int argc, char *argv[]) |
| | | { |
| | | int opt; |
| | | int i = 0; |
| | | //int rv = 0; |
| | | int debug = 0; |
| | | |
| | | char pid_file[64] = { 0 }; /* The file used to record the PID */ |
| | | const char *progname=NULL; |
| | | |
| | | int log_level = LOG_LEVEL_NRML; |
| | | char *log_file = DEF_LOG_FILE; |
| | | logger_t logger; |
| | | |
| | | struct option long_options[] = { |
| | | {"debug", no_argument, NULL, 'd'}, |
| | | {"level", required_argument, NULL, 'l'}, |
| | | {"version", no_argument, NULL, 'v'}, |
| | | {"help", no_argument, NULL, 'h'}, |
| | | {NULL, 0, NULL, 0} |
| | | }; |
| | | |
| | | memset(&logger, 0, sizeof(logger)); |
| | | |
| | | progname = basename(argv[0]); /* get program name */ |
| | | |
| | | /* parser the command line parameters */ |
| | | while ((opt = getopt_long(argc, argv, "c:dl:vh", long_options, NULL)) != -1) |
| | | { |
| | | switch (opt) |
| | | { |
| | | |
| | | case 'd': /* set debug running */ |
| | | debug = 1; |
| | | log_level= LOG_LEVEL_DEBUG; |
| | | log_file = NULL; /* use standard output */ |
| | | break; |
| | | |
| | | case 'l': /* set the log level */ |
| | | i = atoi(optarg); |
| | | log_level = i>LOG_LEVEL_MAX ? LOG_LEVEL_MAX-1 : i; |
| | | logger.flag |= FLAG_LOGGER_LEVEL_OPT; |
| | | break; |
| | | |
| | | case 'v': /* print software version */ |
| | | banner(); |
| | | return EXIT_SUCCESS; |
| | | |
| | | case 'h': /* print help information */ |
| | | program_usage(progname); |
| | | return 0; |
| | | |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | /* check program already running or not, if already running then exit, or set running as daemon */ |
| | | snprintf(pid_file, sizeof(pid_file), "/var/run/%s.pid", progname); |
| | | if( !debug ) |
| | | { |
| | | if( check_daemon_running(pid_file) ) |
| | | { |
| | | printf("Programe already running, exit now.\n"); |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | /* initial and open logger system */ |
| | | if( logger_init(&logger, log_file, log_level, 512)<0 || logger_open()<0 ) |
| | | { |
| | | printf("ERROR: Initialise logger system failure\n"); |
| | | return -1; |
| | | } |
| | | |
| | | /* install signal proc handler */ |
| | | install_proc_signal(); |
| | | |
| | | log_nrml("Program start running\n"); |
| | | |
| | | /* g_signal.stop defined in proc.c, and will be set when catch stop signal */ |
| | | while( !g_signal.stop ) |
| | | { |
| | | log_dbg("Program still running\n"); |
| | | sleep(3); |
| | | } |
| | | |
| | | logger_term(); |
| | | |
| | | return 0; |
| | | } |
| | | |