Changeset 5924:1488a53c9a47 in roaraudio


Ignore:
Timestamp:
09/11/13 12:47:46 (6 years ago)
Author:
phi
Branch:
default
Message:

improve the RoarDMX code a bit: cleanup and better error handling.

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • include/libroarlight/roardmx.h

    r5921 r5924  
    111111 
    112112// Data/high level: 
     113// * *: 
     114int roar_roardmx_message_numchannels(struct roar_roardmx_message * mes); 
     115int roar_roardmx_message_add_chanval(struct roar_roardmx_message * mes, uint16_t   channel, unsigned char   val); 
     116int roar_roardmx_message_get_chanval(struct roar_roardmx_message * mes, uint16_t * channel, unsigned char * val, int index); 
     117 
    113118// * SSET: 
    114119int roar_roardmx_message_new_sset   (struct roar_roardmx_message * mes); 
    115 int roar_roardmx_message_add_chanval(struct roar_roardmx_message * mes, uint16_t   channel, unsigned char   val); 
    116 int roar_roardmx_message_get_chanval(struct roar_roardmx_message * mes, uint16_t * channel, unsigned char * val, int index); 
    117 int roar_roardmx_message_numchannels(struct roar_roardmx_message * mes); 
    118120 
     121// * IPO1: 
     122// Not yet supported. 
     123// * IPO4: 
     124// Not yet supported. 
     125// * INC8S: 
     126// Not yet supported. 
     127// * RANGESET: 
     128// Not yet supported. 
     129// * EVENT: 
     130// Not yet supported. 
     131// * CONTROL: 
     132// Not yet supported. 
    119133 
    120134#endif 
  • libroarlight/roardmx.c

    r5823 r5924  
    2727 
    2828// base(ic) check 
    29 #define BCHK(x) if ( (x) == NULL ) return -1 
     29#define BCHK(x) if ( (x) == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } 
    3030 
    3131int roar_roardmx_message_new (struct roar_roardmx_message * mes) { 
     
    4444//int roar_roardmx_message_get_data(struct roar_roardmx_message * mes, unsigned char ** data); 
    4545 
    46 // mdium level: 
     46// medium level: 
    4747int roar_roardmx_message_set_type(struct roar_roardmx_message * mes, unsigned char   type) { 
    4848 BCHK(mes); 
    4949 
    50  if ( (type | ROAR_ROARDMX_MASK_TYPE) - ROAR_ROARDMX_MASK_TYPE ) 
    51   return -1; 
     50 // check if type contains non-type bits. 
     51 if ( (type | ROAR_ROARDMX_MASK_TYPE) - ROAR_ROARDMX_MASK_TYPE ) { 
     52  roar_err_set(ROAR_ERROR_INVAL); 
     53  return -1; 
     54 } 
    5255 
    5356 mes->type = type; 
     
    8689 BCHK(vio); 
    8790 
    88  if ( mes->length > ROAR_ROARDMX_DATA_LENGTH ) // this is very fatal! 
    89   return -1; 
     91 if ( mes->length > ROAR_ROARDMX_DATA_LENGTH ) { // this is very fatal! 
     92  roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL); 
     93  roar_err_set(ROAR_ERROR_FAULT); 
     94  return -1; 
     95 } 
    9096 
    9197 mes->data[0] =  mes->version; 
     
    110116 mes->version = mes->data[0]; 
    111117 
    112  if ( mes->version != 0 ) 
    113   return -1; 
     118 if ( mes->version != ROAR_ROARDMX_VERSION ) { 
     119  roar_err_set(ROAR_ERROR_NSVERSION); 
     120  return -1; 
     121 } 
    114122 
    115123 mes->flags  = mes->data[1] & ROAR_ROARDMX_MASK_FLAGS; 
     
    125133 
    126134// Data/high level: 
    127 // * SSET: 
    128 int roar_roardmx_message_new_sset   (struct roar_roardmx_message * mes) { 
    129  if ( roar_roardmx_message_new(mes) == -1 ) 
    130   return -1; 
    131  
    132  mes->type = ROAR_ROARDMX_TYPE_SSET; 
    133  
    134  return 0; 
    135 } 
    136  
     135// * *: 
    137136int roar_roardmx_message_add_chanval(struct roar_roardmx_message * mes, uint16_t channel, unsigned char val) { 
    138137 register uint16_t * chan; 
     
    140139 BCHK(mes); 
    141140 
    142  if ( (mes->length + 3) > ROAR_ROARDMX_DATA_LENGTH ) // message would be to long 
    143   return -1; 
     141 switch (mes->type) { 
     142  case ROAR_ROARDMX_TYPE_SSET: 
     143  case ROAR_ROARDMX_TYPE_INC8S: 
     144   break; 
     145  default: 
     146    roar_err_set(ROAR_ERROR_TYPEMM); 
     147    return -1; 
     148   break; 
     149 } 
     150 
     151 if ( (mes->length + 3) > ROAR_ROARDMX_DATA_LENGTH ) { // message would be to long 
     152  roar_err_set(ROAR_ERROR_NOSPC); 
     153  return -1; 
     154 } 
    144155 
    145156 chan = (uint16_t *) &(mes->data[mes->length + 3]); 
     
    159170 BCHK(mes); 
    160171 
    161  if ( index < 0 ) 
    162   return -1; 
    163  
    164  if ( mes->version != 0 ) 
    165   return -1; 
     172 if ( index < 0 ) { 
     173  roar_err_set(ROAR_ERROR_INVAL); 
     174  return -1; 
     175 } 
     176 
     177 if ( mes->version != ROAR_ROARDMX_VERSION ) { 
     178  roar_err_set(ROAR_ERROR_NSVERSION); 
     179  return -1; 
     180 } 
    166181 
    167182 switch (mes->type) { 
     
    178193 } 
    179194 
     195 roar_err_set(ROAR_ERROR_NSTYPE); 
    180196 return -1; 
    181197} 
     
    184200 BCHK(mes); 
    185201 
    186  if ( mes->version != 0 ) 
    187   return -1; 
     202 if ( mes->version != ROAR_ROARDMX_VERSION ) { 
     203  roar_err_set(ROAR_ERROR_NSVERSION); 
     204  return -1; 
     205 } 
    188206 
    189207 switch (mes->type) { 
     
    198216    return mes->length / 12; 
    199217   break; 
    200  } 
    201  
     218  case ROAR_ROARDMX_TYPE_RANGESET: 
     219    return mes->length /  5; 
     220   break; 
     221  case ROAR_ROARDMX_TYPE_EVENT: 
     222  case ROAR_ROARDMX_TYPE_CONTROL: 
     223    return 0; 
     224   break; 
     225 } 
     226 
     227 roar_err_set(ROAR_ERROR_NSTYPE); 
    202228 return -1; 
    203229} 
    204230 
     231// * SSET: 
     232int roar_roardmx_message_new_sset   (struct roar_roardmx_message * mes) { 
     233 if ( roar_roardmx_message_new(mes) == -1 ) 
     234  return -1; 
     235 
     236 mes->type = ROAR_ROARDMX_TYPE_SSET; 
     237 
     238 return 0; 
     239} 
     240 
    205241//ll 
Note: See TracChangeset for help on using the changeset viewer.