static __uint _on_add_edit_tags(PCRTK_PACKET p) { ONLINE_CONFIG::reload_tags_ack *ack; RTK_CURSOR hNode, hTag, hGroup; RTK_TAG *pTag; RTK_GROUP grp; __bool bEdit; __uint tagcount, i; ack = (ONLINE_CONFIG::reload_tags_ack *)p->data; tagcount = p->data_size / sizeof(*ack); if(p->data_size % sizeof(*ack)){ return 0; } ZeroMemory(&grp, sizeof(grp)); if( !lock_rtdb(__true, 100) ){ return 0; } hNode = HNODE_LOCAL_MACHINE; hTag = hGroup = 0; host_to_node(&g_ThisNode->key, &grp.node); if(PACKET_TYPE(p) == PT_EditTag){ bEdit = __true; }else{ bEdit = __false; } for(i=0; i<tagcount; i++){ hGroup = open_group(hNode, &ack[i].tag.group); grp.key = ack[i].tag.group; if(!hGroup){ hGroup = create_group(hNode, &grp); } if(hGroup){ hTag = create_tag(hGroup, &ack[i].tag.key, &ack[i].tag.s); pTag = (RTK_TAG*)cursor_get_item(hTag); if(pTag){ *pTag = ack[i].tag; mark_expired(pTag); close_handle(hTag); if(bEdit){ // add-tag event is auto-fired fire_rtdb_event(EV_ModifyTag, pTag); } } } close_handle(hGroup); } unlock_rtdb(); return tagcount; }
static __uint _on_add_edit_groups(PCRTK_PACKET p) { RTK_CURSOR hNode, hGroup; PRTK_GROUP png, pg=0; __bool bEdit; __uint count; if(p->data_size % sizeof(RTK_GROUP)){ return 0; } if( !lock_rtdb(__true, 100) ){ return 0; } hNode = HNODE_LOCAL_MACHINE; count = p->data_size / sizeof(RTK_GROUP); png = (PRTK_GROUP)p->data; bEdit = PACKET_TYPE(p) == PT_EditGroup? __true : __false; for(__uint i=0; i<count; i++, png++){ hGroup = open_group(hNode, &png->key); pg = (PRTK_GROUP)cursor_get_item(hGroup); if(pg){ memcpy( pg->description, png->description, sizeof(pg->description) ); pg->period = png->period; fire_rtdb_event(EV_ModifyGroup, pg); }else{ hGroup = create_group(hNode, png); pg = (PRTK_GROUP)cursor_get_item(hGroup); } if(pg){ pg->d.flags = png->d.flags; } DEBUG_PRINTF(( "Got group %s, flag=%08x\n", (char *)CGroupName(png->key), png->d.flags )); close_handle(hGroup); } unlock_rtdb(); return count; }
void PMC_API _on_rtdb(PCRTK_PACKET packet, HVBUS bus) { // check for unknown node TAG_NAME tn; PRTK_NODE pNode; #if 0 if(get_power_state() != PWR_RUNNING){ return; } #endif if(g_Worker){ if(lock_rtdb(__false, 100)){ RTK_CURSOR hNode; host_to_node(&packet->src.host, &tn.node); hNode = open_node(&tn.node); pNode = (PRTK_NODE)cursor_get_item(hNode); if(pNode){ pNode->dogcount = g_NodeLife; } close_handle(hNode); unlock_rtdb(); if(!hNode){ memset(&tn.sname, 0, sizeof(tn.sname)); // unknown node g_Worker->inform_load(&tn, 1); } if(!hNode){ return; } } } switch(PACKET_TYPE(packet)){ case PT_ChangedData: update_changed_data(packet, __false); break; case PT_QueryData: _on_queried_data(packet); break; } }
__uint _on_add_edit_tags(PCRTK_PACKET p, bool bGenerateEvents) { ONLINE_CONFIG::reload_tags_ack *ack; RTK_CURSOR hNode, hTag, hGroup; RTK_TAG *pTag; NODE_KEY nodekey; TAG_NAME name; RTK_GROUP grp; __bool bEditing; __uint tagcount, i; ack = (ONLINE_CONFIG::reload_tags_ack *)p->data; tagcount = p->data_size / sizeof(*ack); if(p->data_size % sizeof(*ack)){ return 0; } if(!tagcount){ return 0; } ZeroMemory(&grp, sizeof(grp)); if((PACKET_TYPE(p) == PT_DiscoverTags) && g_Worker){ TAG_NAME tn; memset(&tn, 0, sizeof(tn)); tn.node = ack[0].tag.node; tn.sname.group = ack[0].tag.group; g_Worker->cancel_load(&tn); } if(!lock_rtdb(__true, 100)){ return 0; } host_to_node(&p->src.host, &nodekey); hNode = create_node(&nodekey, 0); hTag = hGroup = 0; name.node = nodekey; grp.node = nodekey; if(hNode){ for(i=0; i<tagcount; i++){ hGroup = open_group(hNode, &ack[i].tag.group); grp.key = ack[i].tag.group; if(!hGroup){ hGroup = create_group(hNode, &grp); } if(hGroup){ bEditing = query_tag_g(hGroup, &ack[i].tag.key)? __true : __false; hTag = create_tag(hGroup, &ack[i].tag.key, &ack[i].tag.s); pTag = (RTK_TAG*)cursor_get_item(hTag); if(pTag){ *pTag = ack[i].tag; /* 2003/5/27, leave tag's dynamic flags as they were 2003/6/24, mark tag as newly received, but the valid flag is kept as set by server */ mark_fresh(pTag); close_handle(hTag); if(bGenerateEvents){ name.sname.group = ack[i].tag.group; name.sname.tag = ack[i].tag.key; rtk_queue_event(PT_AddTag, &name, &p->src); } if(bEditing){ fire_rtdb_event(EV_ModifyTag, pTag); } } } close_handle(hGroup); } close_handle(hNode); } unlock_rtdb(); return tagcount; }
/** * next_packet: * @state: application state structure, * @packet: packet structure to fill, * @buf: buffer to copy packet from, * @buf_len: length of @buf. * * Takes bytes from @buf until a complete raw packet has been seen, * at which point if fills @packet with the decoded information about * it. * * @buf_len is decreased and @buf moved upwards each time bytes are * taken from it. The bytes are copied into an internal buffer so * there's no need to worry about packets crossing block boundaries. * * Returns: 0 if the packet was not complete, 1 if it is complete **/ static int next_packet (CurrentState *state, Packet *packet, const unsigned char **buf, size_t *buf_len) { static unsigned char pbuf[129]; static size_t pbuf_len = 0; int decrypt = 0; /* We need a minimum of two bytes to figure out how long the rest * of it's supposed to be; copy those now if we have room. */ if (pbuf_len < 2) { size_t needed; needed = MIN (*buf_len, 2 - pbuf_len); memcpy (pbuf + pbuf_len, *buf, needed); pbuf_len += needed; *buf += needed; *buf_len -= needed; if (pbuf_len < 2) return 0; } /* We now have the packet header, this is enough information to * figure out how long the rest of it is and whether we need to * decrypt it or not. * * Fill in some of the fields now, ok we'll rewrite these every * time we come through, but that's not really that bad. */ packet->car = PACKET_CAR (pbuf); packet->type = PACKET_TYPE (pbuf); if (packet->car) { switch ((CarPacketType) packet->type) { case CAR_POSITION_UPDATE: packet->len = SPECIAL_PACKET_LEN (pbuf); packet->data = SPECIAL_PACKET_DATA (pbuf); decrypt = 0; break; case CAR_POSITION_HISTORY: packet->len = LONG_PACKET_LEN (pbuf); packet->data = LONG_PACKET_DATA (pbuf); decrypt = 1; break; default: packet->len = SHORT_PACKET_LEN (pbuf); packet->data = SHORT_PACKET_DATA (pbuf); decrypt = 1; break; } } else { switch ((SystemPacketType) packet->type) { case SYS_EVENT_ID: case SYS_KEY_FRAME: packet->len = SHORT_PACKET_LEN (pbuf); packet->data = SHORT_PACKET_DATA (pbuf); decrypt = 0; break; case SYS_TIMESTAMP: packet->len = 2; packet->data = 0; decrypt = 1; break; case SYS_WEATHER: case SYS_TRACK_STATUS: packet->len = SHORT_PACKET_LEN (pbuf); packet->data = SHORT_PACKET_DATA (pbuf); decrypt = 1; break; case SYS_COMMENTARY: case SYS_NOTICE: case SYS_SPEED: packet->len = LONG_PACKET_LEN (pbuf); packet->data = LONG_PACKET_DATA (pbuf); decrypt = 1; break; case SYS_COPYRIGHT: packet->len = LONG_PACKET_LEN (pbuf); packet->data = LONG_PACKET_DATA (pbuf); decrypt = 0; break; case SYS_VALID_MARKER: case SYS_REFRESH_RATE: packet->len = 0; packet->data = 0; decrypt = 0; break; default: info (3, _("Unknown system packet type: %d\n"), packet->type); packet->len = 0; packet->data = 0; decrypt = 0; break; } } /* Copy as much as we can of the rest of the packet */ if (packet->len > 0) { size_t needed; needed = MIN (*buf_len, (packet->len + 2) - pbuf_len); memcpy (pbuf + pbuf_len, *buf, needed); pbuf_len += needed; *buf += needed; *buf_len -= needed; if (pbuf_len < (packet->len + 2)) return 0; } /* We have a full packet, reset our static cache length so we * can re-use it for the next packet (which might happen before * this one has finished being handled when key frames are being * fetched). */ pbuf_len = 0; /* Copy the payload and decrypt it */ if (packet->len > 0) { memcpy (packet->payload, pbuf + 2, packet->len); packet->payload[packet->len] = 0; if (decrypt) decrypt_bytes (state, packet->payload, packet->len); } else { packet->payload[0] = 0; } return 1; }