__uint PMC_API _on_drop_groups(PCRTK_PACKET packet) { __uint count, i, ret=0; RTK_CURSOR hNode, hGroup; PGROUP_KEY pgk; TAG_NAME name; NODE_KEY nk; count = packet->data_size / sizeof(GROUP_KEY); if(packet->data_size % sizeof(GROUP_KEY)){ return 0; } if( !lock_rtdb(true, 100) ){ return 0; } host_to_node(&packet->src.host, &nk); name.node = nk; hNode = open_node(&nk); if(hNode){ for(i=0, pgk=(PGROUP_KEY)packet->data; i<count; i++, pgk++){ name.sname.group = *pgk; hGroup = open_group(hNode, pgk); if(hGroup){ cursor_delete(hGroup); rtk_queue_event(PT_DropGroup, &name, &packet->src); ret++; } } close_handle(hNode); } unlock_rtdb(); return ret; }
/* determine which tags should be saved to history database. */ void CInMemoryBuffer::buildStreamList() { RTK_CURSOR hNode; RTK_CURSOR hGroup; RTK_CURSOR hTag; PCRTK_TAG pTag; TAG_NAME tn; char nodeName[rtkm_node_key_length + 1]; GetPrivateProfileString( "PMC", "ServerName", "LocalServer", nodeName, sizeof(nodeName), get_config_file() ); CNodeName nodeKey(nodeName); //utils_debug("wlock 3\n"); WriteLock(); if(!lock_rtdb(__false, 100)){ //utils_debug("release 6\n"); Release(); return; } // clear list clearStreamList(); hNode = open_node(nodeKey); if(hNode){ hGroup = cursor_open_first_subitem(hNode); while(!cursor_is_end(hGroup)){ hTag = cursor_open_first_subitem(hGroup); while(!cursor_is_end(hTag)){ pTag = (PCRTK_TAG)cursor_get_item(hTag); if(pTag->s.Flags & TF_SaveToHistory){ tn.node = pTag->node; tn.sname.group = pTag->group; tn.sname.tag = pTag->key; addTag(&tn); } cursor_move_next(hTag); } close_handle(hTag); cursor_move_next(hGroup); } close_handle(hGroup); } unlock_rtdb(); //utils_debug("release 7\n"); Release(); }
__uint _fastcall _read_tags( __uint count, PCTAG_NAME names, Ppmc_value_t values, __uint & existed ) { RTK_CURSOR hNode, hTag; NODE_KEY cachedNode; RTK_TAG *pTag; __uint i, valids; valids = 0; existed = 0; ZeroMemory(values, sizeof(values[0]) * count); if(!lock_rtdb(false, 1000)){ return 0; } hNode = 0; hTag = 0; RTK_TIME now; rtk_time_mark(&now); hNode = 0; memset(&cachedNode, 0, sizeof(cachedNode)); for(i=0; i<count; i++){ if(!(cachedNode == names[i].node)){ close_handle(hNode); hNode = 0; } if(!hNode){ hNode = open_node(&names[i].node); cachedNode = names[i].node; } if(!hNode){ values[i].Flags &= ~TF_Valid; continue; } hTag = open_tag(hNode, &names[i].sname); if(hTag){ existed++; pTag = (RTK_TAG*)cursor_get_item(hTag); double diff; diff = rtk_time_diff(&now, &pTag->d.CachedTime); if(diff > (g_fltTagLife*2)){ mark_expired(pTag); } values[i] = pTag->d.Value; valids++; close_handle(hTag); }else{ values[i].Flags &= ~TF_Valid; } } close_handle(hNode); unlock_rtdb(); return valids; }
RTDB_API PRTK_NODE PMC_API query_node( PCNODE_KEY node ) { RTK_CURSOR hNode; PRTK_NODE nd; hNode = open_node(node); nd = (PRTK_NODE)cursor_get_item(hNode); close_handle(hNode); return nd; }
void somi_event_handler(uint8_t event, uint8_t flags) { switch (event) { case EVENT_ACCESS_REQ: if (flags & ACCESS_GRANTED) { open_node(); } else { access_denied_signal(); } break; } }
RTDB_API RTK_CURSOR PMC_API open_group_f( PCNODE_KEY node, PCGROUP_KEY group ) { RTK_CURSOR hNode, hGroup; hNode = open_node(node); if(!hNode){ return 0; } hGroup = open_group(hNode, group); close_handle(hNode); return hGroup; }
RTDB_API PRTK_TAG PMC_API query_tag_f( PCTAG_NAME tag ) { RTK_CURSOR hNode; RTK_TAG * pTag; hNode = open_node(&tag->node); if(!hNode){ return 0; } pTag = query_tag(hNode, &tag->sname); close_handle(hNode); return pTag; }
RTDB_API RTK_CURSOR PMC_API open_tag_f( PCTAG_NAME tag ) { RTK_CURSOR hNode; hNode = open_node(&tag->node); if(!hNode){ return 0; } RTK_CURSOR handle; handle = open_tag(hNode, &tag->sname); close_handle(hNode); return handle; }
/* _on_system() */ void PMC_API _on_system(PCRTK_PACKET packet, HVBUS bus) { #if 0 if(get_power_state() != PWR_RUNNING){ return; } #endif { static __bool priorityBoosted = __false; if(!priorityBoosted){ DWORD boost; boost = GetPrivateProfileInt( "vbus", "SystemBoost", THREAD_PRIORITY_ABOVE_NORMAL, get_config_file() ); SetThreadPriority(GetCurrentThread(), boost); priorityBoosted = __true; } } if(packet->packet_type != PT_ServerClaim){ return; } if(!lock_rtdb(__false, 1000)){ return; } RTK_CURSOR hNode; PRTK_NODE pNode; TAG_NAME tn; 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(!pNode && g_Worker){ memset(&tn.sname, 0, sizeof(tn.sname)); g_Worker->inform_load(&tn, 1); } }
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; } }
void CDBItem::ReloadContents() { CConfigurableItem *item; // discover_groups(&key, 2000); RTK_CURSOR hNode, hGroup; PRTK_GROUP pGroup; hNode = open_node(&key); if(!hNode){ return; } hGroup = cursor_open_first_subitem(hNode); pGroup = (PRTK_GROUP)cursor_get_item(hGroup); while(pGroup){ item = new CGroupItem(&key, &pGroup->key); AddChild(item); cursor_move_next(hGroup); pGroup = (PRTK_GROUP)cursor_get_item(hGroup); } close_handle(hGroup); close_handle(hNode); }
RTDB_API RTK_CURSOR PMC_API create_node(PCNODE_KEY key, __uint context) { RTK_CURSOR handle; RTK_NODE item; handle = open_node(key); if(handle){ return handle; } ZeroMemory(&item, sizeof(item)); item.key = *key; item.context = context; item.itemtable = new GROUP_TABLE; if(!item.itemtable){ return 0; } make_unified_key(item.key); handle = (RTK_CURSOR)_HNODE::create(&g_Nodes, *key, item); if(handle){ FIRE_EVENT(OnAddNode, ((PRTK_NODE)cursor_get_item(handle))); } return handle; }
void TframDB::UpdateView() { int nGroups; int nTags; RTK_CURSOR hNode; hNode = open_node(&key); if(!hNode){ nGroups = 0; }else{ nGroups = cursor_get_subitem_count(hNode); } RTK_CURSOR hGroup; hGroup = cursor_open_first_subitem(hNode); nTags = 0; while(!cursor_is_end(hGroup)){ nTags += cursor_get_subitem_count(hGroup); cursor_move_next(hGroup); } close_handle(hGroup); close_handle(hNode); lblHint->Caption = "分组总数: " + AnsiString(nGroups); lblTagCount->Caption = "标签总数:" + AnsiString(nTags); }
PROXY_API __bool PMC_API discover_groups_ex( PCNODE_KEY key, __uint timeout, __uint * count, __bool bDiscardOld ) { if(count){ *count = 0; } if(bDiscardOld){ // clean database if(!lock_rtdb(__true, timeout/2)){ return 0; } RTK_CURSOR crNode; crNode = open_node(key); if(crNode){ cursor_clear_subitems(crNode); close_handle(crNode); } unlock_rtdb(); } CDiscoverGroups *eye = new CDiscoverGroups(key, timeout); if(!eye){ return __false; } if(!eye->WaitResult()){ return __false; } if(count){ *count = eye->c_items; } return __true; }
__uint _on_queried_data(PCRTK_PACKET p) { RTK_CURSOR hNode, hTag, hGroup; RTK_TAG *pTag; TAG_NAME name; TAG_NAME missing[MAX_MISSING_PERMITED]; __uint nMissing; QUERY_DATA_ENTRY *t; __uint tagcount, retcount, i; t = (QUERY_DATA_ENTRY *)p->data; tagcount = p->data_size / sizeof(*t); if(p->data_size % sizeof(QUERY_DATA_ENTRY)){ return 0; } retcount = 0; if(!lock_rtdb(__false, 100)){ return 0; } memset(&name, 0, sizeof(name)); host_to_node(&p->src.host, &name.node); hNode = open_node(&name.node); hTag = hGroup = 0; if(!hNode){ unlock_rtdb(); if(g_Worker){ g_Worker->inform_load(&name, 1); } return 0; } nMissing = 0; for(i=0; i<tagcount; i++){ hGroup = open_group(hNode, &t[i].name.group); if(hGroup){ hTag = open_tag_g(hGroup, &t[i].name.tag); if(hTag){ pTag = (RTK_TAG*)cursor_get_item(hTag); pTag->d.Value = t[i].value; mark_fresh(pTag); close_handle(hTag); retcount++; }else{ if(g_Worker){ name.sname = t[i].name; missing[nMissing] = name; nMissing++; } } close_handle(hGroup); }else{ if(g_Worker){ missing[nMissing].node = name.node; missing[nMissing].sname.group = t[i].name.group; memset(&missing[nMissing].sname.tag, 0, sizeof(TAG_KEY)); nMissing++; } } } close_handle(hNode); unlock_rtdb(); if(g_Worker && nMissing){ g_Worker->inform_load(missing, nMissing); } return retcount; }
/* Mount one filesystem. */ static error_t do_mount (struct fs *fs, int remount) { error_t err; char *fsopts, *o; size_t fsopts_len; char *mntopts; size_t mntopts_len; fsys_t mounted; inline void explain (const char *command) { if (verbose) { const char *o; printf ("%s %s", command, fs->mntent.mnt_dir); for (o = fsopts; o; o = argz_next (fsopts, fsopts_len, o)) printf (" %s", o); putchar ('\n'); } } err = fs_fsys (fs, &mounted); if (err) { error (0, err, "cannot determine if %s is already mounted", fs->mntent.mnt_fsname); return err; } /* Produce an argz of translator option arguments from the given FS's options and the command-line options. */ #define ARGZ(call) \ err = argz_##call; \ if (err) \ error (3, ENOMEM, "collecting mount options"); \ if (fs->mntent.mnt_opts) { /* Append the fstab options to any specified on the command line. */ ARGZ (create_sep (fs->mntent.mnt_opts, ',', &mntopts, &mntopts_len)); /* Remove the `noauto' and `bind' options, since they're for us not the filesystem. */ for (o = mntopts; o; o = argz_next (mntopts, mntopts_len, o)) { if (strcmp (o, MNTOPT_NOAUTO) == 0) argz_delete (&mntopts, &mntopts_len, o); if (strcmp (o, "bind") == 0) { fs->mntent.mnt_type = strdup ("firmlink"); if (!fs->mntent.mnt_type) error (3, ENOMEM, "failed to allocate memory"); argz_delete (&mntopts, &mntopts_len, o); } } ARGZ (append (&mntopts, &mntopts_len, options, options_len)); } else { mntopts = options; mntopts_len = options_len; } /* Convert the list of options into a list of switch arguments. */ fsopts = 0; fsopts_len = 0; for (o = mntopts; o; o = argz_next (mntopts, mntopts_len, o)) if (*o == '-') /* Allow letter opts `-o -r,-E', BSD style. */ { ARGZ (add (&fsopts, &fsopts_len, o)); } else if ((strcmp (o, "defaults") != 0) && (strlen (o) != 0)) { /* Prepend `--' to the option to make a long option switch, e.g. `--ro' or `--rsize=1024'. */ char arg[2 + strlen (o) + 1]; arg[0] = arg[1] = '-'; memcpy (&arg[2], o, sizeof arg - 2); ARGZ (add (&fsopts, &fsopts_len, arg)); } if (mntopts != options) free (mntopts); #undef ARGZ if (remount) { if (mounted == MACH_PORT_NULL) { error (0, 0, "%s not already mounted", fs->mntent.mnt_fsname); return EBUSY; } /* Send an RPC to request the new options, including --update. */ explain ("fsysopts"); err = fsys_set_options (mounted, fsopts, fsopts_len, 0); if (err) error (0, err, "cannot remount %s", fs->mntent.mnt_fsname); return err; } else { /* Error during file lookup; we use this to avoid duplicating error messages. */ error_t open_err = 0; /* The control port for any active translator we start up. */ fsys_t active_control; file_t node; /* Port to the underlying node. */ struct fstype *type; /* The callback to start_translator opens NODE as a side effect. */ error_t open_node (int flags, mach_port_t *underlying, mach_msg_type_name_t *underlying_type, task_t task, void *cookie) { node = file_name_lookup (fs->mntent.mnt_dir, flags | O_NOTRANS, 0666); if (node == MACH_PORT_NULL) { open_err = errno; return open_err; } *underlying = node; *underlying_type = MACH_MSG_TYPE_COPY_SEND; return 0; } /* Do not fail if there is an active translator if --fake is given. This mimics Linux mount utility more closely which just looks into the mtab file. */ if (mounted != MACH_PORT_NULL && !fake) { error (0, 0, "%s already mounted", fs->mntent.mnt_fsname); return EBUSY; } if (strcmp (fs->mntent.mnt_type, "auto") == 0) { #if HAVE_BLKID char *type = blkid_get_tag_value (NULL, "TYPE", fs->mntent.mnt_fsname); if (! type) { error (0, 0, "failed to detect file system type"); return EFTYPE; } else { if (strcmp (type, "vfat") == 0) fs->mntent.mnt_type = strdup ("fat"); else fs->mntent.mnt_type = strdup (type); if (! fs->mntent.mnt_type) error (3, ENOMEM, "failed to allocate memory"); } #else fs->mntent.mnt_type = strdup ("ext2"); if (! fs->mntent.mnt_type) error (3, ENOMEM, "failed to allocate memory"); #endif } err = fs_type (fs, &type); if (err) { error (0, err, "%s: cannot determine filesystem type", fs->mntent.mnt_fsname); return err; } if (type->program == 0) { error (0, 0, "%s: filesystem type `%s' unknown", fs->mntent.mnt_fsname, type->name); return EFTYPE; } /* Stick the translator program name in front of the option switches. */ err = argz_insert (&fsopts, &fsopts_len, fsopts, type->program); /* Now stick the device name on the end as the last argument. */ if (!err) err = argz_add (&fsopts, &fsopts_len, fs->mntent.mnt_fsname); if (err) error (3, ENOMEM, "collecting mount options"); /* Now we have a translator command line argz in FSOPTS. */ if (fake) { /* Fake the translator startup. */ mach_port_t underlying; mach_msg_type_name_t underlying_type; err = open_node (O_READ, &underlying, &underlying_type, 0, NULL); if (err) error (1, errno, "cannot mount on %s", fs->mntent.mnt_dir); mach_port_deallocate (mach_task_self (), underlying); /* See if the translator is at least executable. */ if (access(type->program, X_OK) == -1) error (1, errno, "can not execute %s", type->program); return 0; } explain ("settrans -a"); err = fshelp_start_translator (open_node, NULL, fsopts, fsopts, fsopts_len, timeout, &active_control); /* If ERR is due to a problem opening the translated node, we print that name, otherwise, the name of the translator. */ if (open_err) error (0, open_err, "cannot mount on %s", fs->mntent.mnt_dir); else if (err) error (0, err, "cannot start translator %s", fsopts); else { err = file_set_translator (node, 0, FS_TRANS_SET|FS_TRANS_EXCL, 0, 0, 0, active_control, MACH_MSG_TYPE_COPY_SEND); if (err == EBUSY) error (0, 0, "%s already mounted on", fs->mntent.mnt_dir); else if (err) error (0, err, "cannot set translator on %s", fs->mntent.mnt_dir); if (err) fsys_goaway (active_control, FSYS_GOAWAY_FORCE); mach_port_deallocate (mach_task_self (), active_control); } return err; }
PROXY_API __uint PMC_API update_changed_data( PCRTK_PACKET packet, __bool bThisIsAServer ) { CHANGED_DATA_ENTRY * t; CHANGED_DATA * cd; __uint i, nMissing; RTK_CURSOR hNode, hGroup, hTag; TAG_NAME name; PRTK_TAG pTag; __uint retcount = 0; TAG_NAME missing[MAX_MISSING_PERMITED]; if(packet->data_size <= sizeof(CHANGED_DATA)){ return 0; } cd = (CHANGED_DATA*)packet->data; t = (CHANGED_DATA_ENTRY*)&cd[1]; if(packet->data_size != cd->count * sizeof(CHANGED_DATA_ENTRY) + sizeof(CHANGED_DATA) ){ return 0; } #if 0 for(i=0; i<cd->count; i++){ utils_debug("%s.%s.%s=%f\n", (char*)CHostName(packet->src.host), (char*)CGroupName(cd->group), (char*)CTagName(t[i].name), t[i].value.Value.fltValue ); } #endif memset(&name, 0, sizeof(name)); host_to_node(&packet->src.host, &name.node); name.sname.group = cd->group; if(!lock_rtdb(__false, 100)){ return 0; } if(bThisIsAServer){ if(!(packet->src.host == g_ThisNode->key)){ unlock_rtdb(); return 0; } hNode = HNODE_LOCAL_MACHINE; }else{ hNode = open_node(&name.node); } if(!hNode){ unlock_rtdb(); return 0; } hGroup = open_group(hNode, &cd->group); if(!hGroup){ if(!bThisIsAServer){ close_handle(hNode); } unlock_rtdb(); if(g_Worker){ g_Worker->inform_load(&name, 1); } // 2003/5/27, in current implementation all following // tags are guaranteed to be in the same group, so there // is no need to proceed. return 0; } nMissing = 0; for(i=0; i<cd->count; i++){ hTag = open_tag_g(hGroup, &t[i].name); if(hTag){ pTag = (PRTK_TAG)cursor_get_item(hTag); pTag->d.Value = t[i].value; // pTag->d.Value.Flags = t[i].value.Flags; mark_fresh(pTag); close_handle(hTag); retcount++; }else{ if(g_Worker && nMissing < MAX_MISSING_PERMITED){ name.sname.tag = t[i].name; missing[nMissing] = name; nMissing++; } } } close_handle(hGroup); if(!bThisIsAServer){ close_handle(hNode); } unlock_rtdb(); if(nMissing && g_Worker){ if(nMissing > 16){ memset(&missing[0].sname.tag, 0, sizeof(TAG_KEY)); g_Worker->inform_load(missing, 1); }else{ g_Worker->inform_load(missing, nMissing); } } return retcount; }
__uint _on_add_edit_groups(PCRTK_PACKET p, bool /* bGenerateEvents */) { RTK_CURSOR hNode, hGroup; NODE_KEY nk; PRTK_GROUP png, pg=0; __uint count; if(p->data_size % sizeof(RTK_GROUP)){ return 0; } if(!lock_rtdb(__true, 100)){ return 0; } host_to_node(&p->src.host, &nk); hNode = open_node(&nk); if(!hNode){ unlock_rtdb(); return 0; } count = p->data_size / sizeof(RTK_GROUP); png = (PRTK_GROUP)p->data; 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; }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); if(pg /* && bGenerateEvents */){ TAG_NAME name; name.node = nk; name.sname.group = png->key; rtk_queue_event( PT_AddGroup, &name, &p->src ); } } close_handle(hNode); unlock_rtdb(); return count; }
NodeOpener(Node *node, int mode) { fFD = open_node(node, mode); }
extern "C" void TIM2_IRQHandler() { open_node(); if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); uint8_t pcd = RC522_PCD_1; do { // Chose rfid device. rc522_pcd_select(pcd); uint8_t status = mfrc522_read(Status1Reg); __disable_irq(); // If timer is not running and interrupt timer flag is not active reinit device. if (!(status & TRunning)) { uint8_t need_reinit = 0; switch (pcd) { case RC522_PCD_1: if (EXTI_GetITStatus(EXTI_Line10) == RESET) need_reinit = 1; break; case RC522_PCD_2: if (EXTI_GetITStatus(EXTI_Line11) == RESET) need_reinit = 1; break; } if (need_reinit) { spi_hardware_failure_signal(); mfrc522_init(); __enable_irq(); rc522_irq_prepare(); } } __enable_irq(); } while (pcd++ < RC522_PCD_2); // Check enc28j60 chip and restart if needed. if (!enc28j60_revid || (enc28j60_revid != enc28j60_rcr(EREVID)) || (GPIO_ReadInputDataBit(ETH_GPIO, ETH_IRQ_PIN) == RESET && (EXTI_GetITStatus(EXTI_Line2) == RESET))) { enc28j60_init(mac_addr); } uint16_t phstat1 = enc28j60_read_phy(PHSTAT1); // Если ethernet провод вытаскивали, обновить DHCP. // Пока отключено, т.к. по непонятным причинам LLSTAT падает иногда // хотя коннект сохраняется, что вызывает провалы в доступности интерфейса на 3-10 секунд, // пока интерфейс не поднимится по DHCP заного, однако если согласно LLSTAT линк выключен // но мы не гасим интерфейс он продолжает нормально работать. if(!(phstat1 & PHSTAT1_LLSTAT)) { static uint16_t link_dhcp_time; // Avoid frequently link checks. if (ticks - link_dhcp_time > 5000) { link_dhcp_time = ticks; // Обновим адрес через 5 секунд // (после того, как линк появится) dhcp_status = DHCP_INIT; dhcp_retry_time = RTC_GetCounter() + 2; // Линка нет - опускаем интерфейс ip_addr = 0; ip_mask = 0; ip_gateway = 0; enc28j60_init(mac_addr); } } } }
extern "C" void SysTick_Handler(void) { ticks++; open_node(); }
extern "C" void EXTI0_IRQHandler() { EXTI_ClearITPendingBit(EXTI_Line0); open_node(); }