svghmi/svghmi.c
branchsvghmi
changeset 2789 ba0dd2ec6dc4
parent 2788 2ed9ff826d03
child 2798 ddb2c4668a6b
equal deleted inserted replaced
2788:2ed9ff826d03 2789:ba0dd2ec6dc4
    58 
    58 
    59 static hmi_tree_item_t hmi_tree_item[] = {
    59 static hmi_tree_item_t hmi_tree_item[] = {
    60 %(variable_decl_array)s
    60 %(variable_decl_array)s
    61 };
    61 };
    62 
    62 
    63 typedef int(*hmi_tree_iterator)(uint32_t*, hmi_tree_item_t*);
    63 typedef int(*hmi_tree_iterator)(uint32_t, hmi_tree_item_t*);
    64 static int traverse_hmi_tree(hmi_tree_iterator fp)
    64 static int traverse_hmi_tree(hmi_tree_iterator fp)
    65 {
    65 {
    66     unsigned int i;
    66     unsigned int i;
    67     for(i = 0; i < sizeof(hmi_tree_item)/sizeof(hmi_tree_item_t); i++){
    67     for(i = 0; i < sizeof(hmi_tree_item)/sizeof(hmi_tree_item_t); i++){
    68         int res;
    68         int res;
    74 
    74 
    75 #define __Unpack_desc_type hmi_tree_item_t
    75 #define __Unpack_desc_type hmi_tree_item_t
    76 
    76 
    77 %(var_access_code)s
    77 %(var_access_code)s
    78 
    78 
    79 inline int write_iterator(uint32_t index, hmi_tree_item_t *dsc)
    79 static int write_iterator(uint32_t index, hmi_tree_item_t *dsc)
    80 {
    80 {
    81     if(AtomicCompareExchange(&dsc->wlock, 0, 1) == 0)
    81     if(AtomicCompareExchange(&dsc->wlock, 0, 1) == 0)
    82     {
    82     {
    83         if(dsc->wstate == buf_set){
    83         if(dsc->wstate == buf_set){
    84             /* if being subscribed */
    84             /* if being subscribed */
   112     }
   112     }
   113     // else ... : PLC can't wait, variable will be updated next turn
   113     // else ... : PLC can't wait, variable will be updated next turn
   114     return 0;
   114     return 0;
   115 }
   115 }
   116 
   116 
   117 inline int send_iterator(uint32_t index, hmi_tree_item_t *dsc)
   117 static int send_iterator(uint32_t index, hmi_tree_item_t *dsc)
   118 {
   118 {
   119     int res = 0;
   119     int res = 0;
   120     while(AtomicCompareExchange(&dsc->wlock, 0, 1)) sched_yield();
   120     while(AtomicCompareExchange(&dsc->wlock, 0, 1)) sched_yield();
   121 
   121 
   122     if(dsc->wstate == buf_tosend)
   122     if(dsc->wstate == buf_tosend)
   139 
   139 
   140     AtomicCompareExchange(&dsc->wlock, 1, 0);
   140     AtomicCompareExchange(&dsc->wlock, 1, 0);
   141     return res; 
   141     return res; 
   142 }
   142 }
   143 
   143 
   144 inline int read_iterator(uint32_t index, hmi_tree_item_t *dsc)
   144 static int read_iterator(uint32_t index, hmi_tree_item_t *dsc)
   145 {
   145 {
   146     if(AtomicCompareExchange(&dsc->rlock, 0, 1) == 0)
   146     if(AtomicCompareExchange(&dsc->rlock, 0, 1) == 0)
   147     {
   147     {
   148         if(dsc->rstate == buf_set)
   148         if(dsc->rstate == buf_set)
   149         {
   149         {
   165     while(AtomicCompareExchange(&dsc->wlock, 0, 1)) sched_yield();
   165     while(AtomicCompareExchange(&dsc->wlock, 0, 1)) sched_yield();
   166     dsc->refresh_period_ms = refresh_period_ms;
   166     dsc->refresh_period_ms = refresh_period_ms;
   167     AtomicCompareExchange(&dsc->wlock, 1, 0);
   167     AtomicCompareExchange(&dsc->wlock, 1, 0);
   168 }
   168 }
   169 
   169 
   170 inline int reset_iterator(uint32_t index, hmi_tree_item_t *dsc)
   170 static int reset_iterator(uint32_t index, hmi_tree_item_t *dsc)
   171 {
   171 {
   172     update_refresh_period(*dsc, 0);
   172     update_refresh_period(dsc, 0);
   173     return 0;
   173     return 0;
   174 }
   174 }
   175 
   175 
   176 static pthread_cond_t svghmi_send_WakeCond = PTHREAD_COND_INITIALIZER;
   176 static pthread_cond_t svghmi_send_WakeCond = PTHREAD_COND_INITIALIZER;
   177 static pthread_mutex_t svghmi_send_WakeCondLock = PTHREAD_MUTEX_INITIALIZER;
   177 static pthread_mutex_t svghmi_send_WakeCondLock = PTHREAD_MUTEX_INITIALIZER;
   248 
   248 
   249 int svghmi_recv_dispatch(uint32_t size, const uint8_t *ptr){
   249 int svghmi_recv_dispatch(uint32_t size, const uint8_t *ptr){
   250     const uint8_t* cursor = ptr + HMI_HASH_SIZE;
   250     const uint8_t* cursor = ptr + HMI_HASH_SIZE;
   251     const uint8_t* end = ptr + size;
   251     const uint8_t* end = ptr + size;
   252 
   252 
   253     printf("svghmi_recv_dispatch %d\n",size);
   253     printf("svghmi_recv_dispatch %%d\n",size);
   254 
   254 
   255     /* match hmitree fingerprint */
   255     /* match hmitree fingerprint */
   256     if(size <= HMI_HASH_SIZE || memcmp(ptr, hmihash, HMI_HASH_SIZE) != 0)
   256     if(size <= HMI_HASH_SIZE || memcmp(ptr, hmi_hash, HMI_HASH_SIZE) != 0)
   257     {
   257     {
   258         printf("svghmi_recv_dispatch MISMATCH !!\n");
   258         printf("svghmi_recv_dispatch MISMATCH !!\n");
   259         return EINVAL;
   259         return EINVAL;
   260     }
   260     }
   261 
   261 
   266         switch(cmd)
   266         switch(cmd)
   267         {
   267         {
   268             case setval:
   268             case setval:
   269             {
   269             {
   270                 uint32_t index = *(uint32_t*)(cursor);
   270                 uint32_t index = *(uint32_t*)(cursor);
   271                 uint8_t *valptr = cursor + sizeof(uint32_t);
   271                 uint8_t const *valptr = cursor + sizeof(uint32_t);
   272 
   272 
   273                 if(index < HMI_ITEM_COUNT)
   273                 if(index < HMI_ITEM_COUNT)
   274                 {
   274                 {
   275                     hmi_tree_item_t *dsc = &hmi_tree_item[index];
   275                     hmi_tree_item_t *dsc = &hmi_tree_item[index];
   276                     void *real_value_p = NULL;
   276                     void *real_value_p = NULL;
   277                     char flags = 0;
   277                     char flags = 0;
   278                     void *visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
   278                     void *visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
   279                     void *dst_p = &rbuf[dsc->buf_index];
   279                     void *dst_p = &rbuf[dsc->buf_index];
   280                     uint32_t sz = __get_type_enum_size(dsc->type);
   280                     uint32_t sz = __get_type_enum_size(dsc->type);
   281 
   281 
   282                     if(valptr + sz < end)
   282                     if((valptr + sz) < end)
   283                     {
   283                     {
   284                         // rescheduling spinlock until free
   284                         // rescheduling spinlock until free
   285                         while(AtomicCompareExchange(&dsc->rlock, 0, 1)) sched_yield();
   285                         while(AtomicCompareExchange(&dsc->rlock, 0, 1)) sched_yield();
   286 
   286 
   287                         memcpy(dst_p, valptr, sz);
   287                         memcpy(dst_p, valptr, sz);
   309                 uint16_t refresh_period_ms = *(uint32_t*)(cursor + sizeof(uint32_t));
   309                 uint16_t refresh_period_ms = *(uint32_t*)(cursor + sizeof(uint32_t));
   310 
   310 
   311                 if(index < HMI_ITEM_COUNT)
   311                 if(index < HMI_ITEM_COUNT)
   312                 {
   312                 {
   313                     hmi_tree_item_t *dsc = &hmi_tree_item[index];
   313                     hmi_tree_item_t *dsc = &hmi_tree_item[index];
   314                     update_refresh_period(*dsc, refresh_period_ms);
   314                     update_refresh_period(dsc, refresh_period_ms);
   315                 }
   315                 }
   316                 else return -EINVAL;
   316                 else return -EINVAL;
   317 
   317 
   318                 progress = sizeof(uint32_t) /* index */ + 
   318                 progress = sizeof(uint32_t) /* index */ + 
   319                            sizeof(uint16_t) /* refresh period */;
   319                            sizeof(uint16_t) /* refresh period */;
   320             }
   320             }
   321             break;
   321             break;
   322 
   322 
   323             case unsubscribe:
   323             case unsubscribe:
   324             {
   324             {
       
   325                 uint32_t index = *(uint32_t*)(cursor);
       
   326 
   325                 if(index < HMI_ITEM_COUNT)
   327                 if(index < HMI_ITEM_COUNT)
   326                 {
   328                 {
   327                     hmi_tree_item_t *dsc = &hmi_tree_item[index];
   329                     hmi_tree_item_t *dsc = &hmi_tree_item[index];
   328                     reset_iterator(index, dsc);
   330                     reset_iterator(index, dsc);
   329                 }
   331                 }