/* * This function assumes the caller has allocated and populated the * shared_mr->pages array with pinned pages for the memory region * and set shared_mr->page_cnt. */ int det_reg_shared(struct det_mr * const mr, const struct det_sh_reg * const sh_reg, __u32 * const l_key, net32_t * const r_key, struct det_mr * const shared_mr) { struct det_device *detdev = mr->base.detdev; int i, shared_key, err; if (unlikely(((sh_reg->access & DET_AC_REMOTE_READ) && !(sh_reg->access & DET_AC_LOCAL_READ)) || ((sh_reg->access & DET_AC_REMOTE_WRITE) && !(sh_reg->access & DET_AC_LOCAL_WRITE)))) return -EINVAL; if (unlikely(detdev->mr_cnt >= MAX_MRS)) return -EAGAIN; do { if (unlikely(!det_idr_pre_get(&det_wire_map, GFP_KERNEL))) return -ENOMEM; wiremap_write_lock_bh(); err = det_get_id(&det_wire_map, shared_mr, &shared_key); wiremap_write_unlock_bh(); } while (unlikely(err == -EAGAIN)); if (unlikely(err)) return err; shared_mr->base.type = DET_TYPE_MR; shared_mr->base.detdev = detdev; det_spin_lock_init(&shared_mr->lock); shared_mr->vaddr = sh_reg->vaddr; shared_mr->attr.l_key = shared_key; shared_mr->attr.base.length = mr->attr.base.length; shared_mr->attr.base.access = sh_reg->access; shared_mr->attr.base.pd = sh_reg->pd; shared_mr->attr.base.r_key = (sh_reg->access & (DET_AC_REMOTE_READ | DET_AC_REMOTE_WRITE)) ? HTON32(shared_key) : HTON32(0); init_completion(&shared_mr->base.done); atomic_set(&shared_mr->base.refcnt, 1); atomic_set(&shared_mr->windows, 0); atomic_inc(&sh_reg->pd->refcnt); /* Take a reference on the pages. */ for (i = 0; i < shared_mr->page_cnt; i++) get_page(shared_mr->pages[i]); *l_key = shared_mr->attr.l_key; if (r_key) *r_key = shared_mr->attr.base.r_key; write_lock(&detdev->lock); list_add_tail(&shared_mr->base.entry, &detdev->mr_list); detdev->mr_cnt++; write_unlock(&detdev->lock); return 0; }
int det_modify_mr(struct det_mr * const mr, const struct det_mr_mod * const mr_mod, __u32 * const l_key, net32_t * const r_key) { int mr_key, err; if (atomic_read(&mr->windows)) return -EBUSY; if ((mr_mod->flags & DET_MR_MOD_ACCESS) && (unlikely(((mr_mod->access & DET_AC_REMOTE_READ) && !(mr_mod->access & DET_AC_LOCAL_READ)) || ((mr_mod->access & DET_AC_REMOTE_WRITE) && !(mr_mod->access & DET_AC_LOCAL_WRITE))))) return -EINVAL; do { if (unlikely(!det_idr_pre_get(&det_wire_map, GFP_KERNEL))) return -ENOMEM; wiremap_write_lock_bh(); err = det_get_id(&det_wire_map, mr, &mr_key); wiremap_write_unlock_bh(); } while (unlikely(err == -EAGAIN)); if (unlikely(err)) return err; mr_lock(&mr->lock); wiremap_write_lock_bh(); idr_remove(&det_wire_map, mr->attr.l_key); wiremap_write_unlock_bh(); mr->attr.l_key = mr_key; mr->attr.base.r_key = (mr_mod->access & (DET_AC_REMOTE_READ | DET_AC_REMOTE_WRITE)) ? HTON32(mr_key) : HTON32(0); if (mr_mod->flags & DET_MR_MOD_PD) { atomic_dec(&mr->attr.base.pd->refcnt); mr->attr.base.pd = mr_mod->pd; atomic_inc(&mr->attr.base.pd->refcnt); } if (mr_mod->flags & DET_MR_MOD_ACCESS) mr->attr.base.access = mr_mod->access; *l_key = mr->attr.l_key; if (r_key) *r_key = mr->attr.base.r_key; mr_unlock(&mr->lock); return 0; }
int det_create_mw(struct det_pd * const pd, struct det_mw * const mw) { struct det_device *detdev = pd->detdev; if (unlikely(detdev->mw_cnt >= MAX_MWS)) return -EAGAIN; mw->base.type = DET_TYPE_MW; mw->base.detdev = detdev; mw->mr = NULL; mw->attr.base.pd = pd; mw->attr.base.access = 0; mw->attr.base.length = 0; mw->attr.base.r_key = HTON32(0); init_completion(&mw->base.done); atomic_set(&mw->base.refcnt, 1); atomic_inc(&pd->refcnt); write_lock(&detdev->lock); list_add_tail(&mw->base.entry, &detdev->mw_list); detdev->mw_cnt++; write_unlock(&detdev->lock); return 0; }
TWiMDLRResultcodes iM880A_DirectDeviceActivation(uint32_t deviceAddress, uint8_t* nwkSessionKey, uint8_t* appSessionKey) { HTON32(&TxMessage.Payload[0], deviceAddress); if(nwkSessionKey) { uint8_t* dstPtr = TxMessage.Payload + DEVICE_ADDR_LEN; int n = KEY_LEN; // copy bytes while(n--) *dstPtr++ = *nwkSessionKey++; } if(appSessionKey) { uint8_t* dstPtr = TxMessage.Payload + DEVICE_ADDR_LEN + KEY_LEN; int n = KEY_LEN; // copy bytes while(n--) *dstPtr++ = *appSessionKey++; } return (TWiMDLRResultcodes) iM880A_SendHCIMessage(LORAWAN_ID, LORAWAN_MSG_ACTIVATE_DEVICE_REQ, NULL, DEVICE_ADDR_LEN + KEY_LEN + KEY_LEN); }
/*read 4 byte*/ int ReadU32(uint32_t *u32, FILE *fp) { if (fread(u32, 4, 1, fp) != 1) { return 0; } *u32 = HTON32(*u32); return 1; }
/* Serialize ppr data of each bandwidth into the given buffer, returns bytes copied */ static uint ppr_serialize_data(const ppr_t *pprptr, uint8* buf, uint32 serflag) { uint ret = sizeof(ppr_deser_header_t); ppr_deser_header_t* header = (ppr_deser_header_t*)buf; ASSERT(pprptr && buf); header->version = PPR_SERIALIZATION_VER; header->bw = (uint8)pprptr->ch_bw; header->flags = HTON32(ppr_get_flag()); buf += sizeof(*header); switch (header->bw) { case WL_TX_BW_20: { const uint8* pprbuf = (const uint8*)&pprptr->ppr_bw.ch20.b20; ret += ppr_serialize_block(pprbuf, &buf, serflag); } break; case WL_TX_BW_40: { const uint8* pprbuf = (const uint8*)&pprptr->ppr_bw.ch40.b40; ret += ppr_serialize_block(pprbuf, &buf, serflag); pprbuf = (const uint8 *) &pprptr->ppr_bw.ch40.b20in40; ret += ppr_serialize_block(pprbuf, &buf, serflag); } break; case WL_TX_BW_80: { const uint8* pprbuf = (const uint8*)&pprptr->ppr_bw.ch80.b80; ret += ppr_serialize_block(pprbuf, &buf, serflag); pprbuf = (const uint8*)&pprptr->ppr_bw.ch80.b20in80; ret += ppr_serialize_block(pprbuf, &buf, serflag); pprbuf = (const uint8*)&pprptr->ppr_bw.ch80.b40in80; ret += ppr_serialize_block(pprbuf, &buf, serflag); } break; case WL_TX_BW_ALL: { const uint8* pprbuf = (const uint8*)&pprptr->ppr_bw.all.b20; ret += ppr_serialize_block(pprbuf, &buf, serflag); pprbuf = (const uint8*)&pprptr->ppr_bw.all.b40; ret += ppr_serialize_block(pprbuf, &buf, serflag); pprbuf = (const uint8*)&pprptr->ppr_bw.all.b20in40; ret += ppr_serialize_block(pprbuf, &buf, serflag); pprbuf = (const uint8*)&pprptr->ppr_bw.all.b80; ret += ppr_serialize_block(pprbuf, &buf, serflag); pprbuf = (const uint8*)&pprptr->ppr_bw.all.b20in80; ret += ppr_serialize_block(pprbuf, &buf, serflag); pprbuf = (const uint8*)&pprptr->ppr_bw.all.b40in80; ret += ppr_serialize_block(pprbuf, &buf, serflag); } break; default: ASSERT(0); } return ret; }
/* Constructor routine for opaque PPR struct */ ppr_t* ppr_create(osl_t *osh, wl_tx_bw_t bw) { ppr_t* pprptr; ASSERT((bw == WL_TX_BW_20) || (bw == WL_TX_BW_40) || (bw == WL_TX_BW_80) || (bw == WL_TX_BW_ALL)); #ifndef BCMDRIVER BCM_REFERENCE(osh); if ((pprptr = (ppr_t*)malloc((uint)ppr_size(bw))) != NULL) { #else if ((pprptr = (ppr_t*)MALLOC(osh, (uint)ppr_size(bw))) != NULL) { #endif ppr_init(pprptr, bw); } return pprptr; } /* Init flags in the memory block for serialization, the serializer will check * the flag to decide which ppr to be copied */ int ppr_init_ser_mem_by_bw(uint8* pbuf, wl_tx_bw_t bw, uint32 len) { ppr_ser_mem_flag_t *pmflag; if (pbuf == NULL || ppr_ser_size_by_bw(bw) > len) return BCME_BADARG; pmflag = (ppr_ser_mem_flag_t *)pbuf; pmflag->magic_word = HTON32(PPR_SER_MEM_WORD); pmflag->flag = HTON32(ppr_get_flag()); /* init the memory */ memset(pbuf + sizeof(*pmflag), (uint8)WL_RATE_DISABLED, len-sizeof(*pmflag)); return BCME_OK; }
static void det_unbind_mw(struct det_mw * const mw) { /* Determine if the MW is currently bound. */ if (!mw->attr.base.length) return; /* Remove the r_key from the map. */ wiremap_write_lock_bh(); idr_remove(&det_wire_map, NTOH32(mw->attr.base.r_key)); wiremap_write_unlock_bh(); atomic_dec(&mw->mr->windows); mw->mr = NULL; mw->attr.base.length = 0; mw->attr.base.access = 0; mw->attr.base.r_key = HTON32(0); }
bool ZFile::Write32(int i32) { i32=HTON32(i32); WriteData(&i32,4); return true; }
int det_bind_mw(struct det_mw * const mw, struct det_mr * const mr, net32_t * const r_key, const __u64 vaddr, const __u32 length, const enum det_access_ctrl access) { int mr_key, err; /* * A previously bound MW can be bound to a new virtual address * range in the same or a different MR, causing the previous * binding to be invalidated. Binding a previously bound MW * to a zero-length virtual address range will invalidate the * previous binding and return an r_key that is unbound. */ det_unbind_mw(mw); /* Determine if the MW was just being unbound. */ if (!length) { /* Return the unbound r_key to the client. */ *r_key = mw->attr.base.r_key; return 0; } if (unlikely((length < MIN_MR_SIZE) || (length > MAX_MR_SIZE))) return -EINVAL; /* The MR and MW must belong to the same PD. */ if (unlikely(mr->attr.base.pd != mw->attr.base.pd)) return -EPERM; /* Perform immediate checks on MR address and length. */ if (unlikely((vaddr < mr->vaddr) || ((vaddr + (length - 1)) < vaddr) || ((vaddr + length) > (mr->vaddr + mr->attr.base.length)))) return -ERANGE; /* Verify the MW and MR access rights. */ if (unlikely(!(access & (DET_AC_REMOTE_READ | DET_AC_REMOTE_WRITE)) || !(mr->attr.base.access & DET_AC_MW_BIND) || ((access & DET_AC_REMOTE_WRITE) && !(mr->attr.base.access & DET_AC_LOCAL_WRITE)) || ((access & DET_AC_REMOTE_READ) && !(mr->attr.base.access & DET_AC_LOCAL_READ)))) return -EACCES; /* Bind the MW. */ mw->mr = mr; mw->mr_offset = vaddr - mr->vaddr; mw->attr.base.length = length; mw->attr.base.access = access; do { if (unlikely(!det_idr_pre_get(&det_wire_map, GFP_KERNEL))) return -ENOMEM; wiremap_write_lock_bh(); err = det_get_id(&det_wire_map, mw, &mr_key); wiremap_write_unlock_bh(); } while (unlikely(err == -EAGAIN)); if (unlikely(err)) return err; mw->attr.base.r_key = HTON32(mr_key); /* Return the r_key to the client. */ *r_key = mw->attr.base.r_key; /* * Increment the window count on the MR. * It will be decremented when the memory window is unbound. */ atomic_inc(&mr->windows); return 0; }
/* * This function assumes the caller has allocated and populated the mr->pages * array with pinned pages for the memory region and set mr->page_cnt. */ int det_reg_mr(struct det_pd * const pd, const struct det_mr_reg * const mr_reg, __u32 * const l_key, net32_t * const r_key, struct det_mr * const mr) { struct det_device *detdev = pd->detdev; int i, mr_key, err; if (unlikely(((mr_reg->access & DET_AC_REMOTE_READ) && !(mr_reg->access & DET_AC_LOCAL_READ)) || ((mr_reg->access & DET_AC_REMOTE_WRITE) && !(mr_reg->access & DET_AC_LOCAL_WRITE)))) return -EINVAL; if (unlikely((mr_reg->length < MIN_MR_SIZE) || (mr_reg->length > MAX_MR_SIZE))) return -EINVAL; if (unlikely((mr_reg->vaddr + (mr_reg->length - 1)) < mr_reg->vaddr)) return -ERANGE; if (unlikely(detdev->mr_cnt >= MAX_MRS)) return -EAGAIN; /* Restrict to percentage of kernel pages. */ if (mr->page_cnt > (det_max_pages - atomic_read(&det_page_count))) return -EDQUOT; do { if (unlikely(!det_idr_pre_get(&det_wire_map, GFP_KERNEL))) return -ENOMEM; wiremap_write_lock_bh(); err = det_get_id(&det_wire_map, mr, &mr_key); wiremap_write_unlock_bh(); } while (unlikely(err == -EAGAIN)); if (unlikely(err)) return err; mr->base.type = DET_TYPE_MR; mr->base.detdev = detdev; det_spin_lock_init(&mr->lock); mr->vaddr = mr_reg->vaddr; mr->attr.l_key = mr_key; mr->attr.base.length = mr_reg->length; mr->attr.base.access = mr_reg->access; mr->attr.base.pd = pd; mr->attr.base.r_key = (mr_reg->access & (DET_AC_REMOTE_READ | DET_AC_REMOTE_WRITE)) ? HTON32(mr_key) : HTON32(0); init_completion(&mr->base.done); atomic_set(&mr->base.refcnt, 1); atomic_set(&mr->windows, 0); atomic_inc(&pd->refcnt); atomic_add(mr->page_cnt, &det_page_count); /* Take a reference on the pages. */ for (i = 0; i < mr->page_cnt; i++) get_page(mr->pages[i]); *l_key = mr->attr.l_key; if (r_key) *r_key = mr->attr.base.r_key; write_lock(&detdev->lock); list_add_tail(&mr->base.entry, &detdev->mr_list); detdev->mr_cnt++; write_unlock(&detdev->lock); return 0; }
/**************************************************************************************************************** * Function: SendNetworkMessage * Desc: Sends the given message over the connection. * Params: * h - Pointer to URLContext struct used to store all connection info associated with this connection * message - Pointer to the message to be sent * Return: * 0 on success, non 0 on failure ****************************************************************************************************************/ static int SendNetworkMessage( URLContext *h, NetworkMessage *message ) { unsigned char messageBuffer[DS_WRITE_BUFFER_SIZE]; int bufIdx = SIZEOF_MESSAGE_HEADER_IO; /* 0 the buffer */ memset( messageBuffer, 0, DS_WRITE_BUFFER_SIZE ); /* Write the header into the buffer */ HToNMessageHeader( &message->header, messageBuffer ); /* Now write the rest of the message to the buffer based on its type */ switch( message->header.messageType ) { case TCP_CLI_CONNECT: { ClientConnectMsg tempMsg; tempMsg.udpPort = HTON32(((ClientConnectMsg *)message->body)->udpPort); memcpy( &messageBuffer[bufIdx], &tempMsg.udpPort, sizeof(unsigned long) ); bufIdx += sizeof(unsigned long); tempMsg.connectType = HTON32(((ClientConnectMsg *)message->body)->connectType); memcpy( &messageBuffer[bufIdx], &tempMsg.connectType, sizeof(long) ); bufIdx += sizeof(long); memcpy( &messageBuffer[bufIdx], ((ClientConnectMsg *)message->body)->userID, MAX_USER_ID_LENGTH ); bufIdx += MAX_USER_ID_LENGTH; memcpy( &messageBuffer[bufIdx], ((ClientConnectMsg *)message->body)->accessKey, ACCESS_KEY_LENGTH ); bufIdx += ACCESS_KEY_LENGTH; } break; case TCP_CLI_IMG_LIVE_REQUEST: { long temp; temp = HTON32( ((CliImgLiveRequestMsg*)message->body)->cameraMask ); memcpy( &messageBuffer[bufIdx], &temp, sizeof(long) ); bufIdx += sizeof(long); temp = HTON32( ((CliImgLiveRequestMsg*)message->body)->resolution ); memcpy( &messageBuffer[bufIdx], &temp, sizeof(long) ); bufIdx += sizeof(long); } break; case TCP_CLI_IMG_PLAY_REQUEST: { long temp; int64_t tempBig; temp = HTON32( ((CliImgPlayRequestMsg*)message->body)->cameraMask ); memcpy( &messageBuffer[bufIdx], &temp, sizeof(long) ); bufIdx += sizeof(long); temp = HTON32( ((CliImgPlayRequestMsg*)message->body)->mode ); memcpy( &messageBuffer[bufIdx], &temp, sizeof(long) ); bufIdx += sizeof(long); temp = HTON32( ((CliImgPlayRequestMsg*)message->body)->pace ); memcpy( &messageBuffer[bufIdx], &temp, sizeof(long) ); bufIdx += sizeof(long); tempBig = HTON64( ((CliImgPlayRequestMsg*)message->body)->fromTime ); memcpy( &messageBuffer[bufIdx], &tempBig, sizeof(int64_t) ); bufIdx += sizeof(int64_t); tempBig = HTON64( ((CliImgPlayRequestMsg*)message->body)->toTime ); memcpy( &messageBuffer[bufIdx], &tempBig, sizeof(int64_t) ); bufIdx += sizeof(int64_t); } break; } /* Write to output stream - remember to add on the 4 bytes for the magic number which precedes the length */ return DSWrite( h, messageBuffer, message->header.length + sizeof(unsigned long) ); }