Exemplo n.º 1
0
static void remove_node_left(SgBTreeNode **root, SgBTreeNode *n)
{
    SgBTreeNode *l = n->left, *r = n->right, *p = n->parent;
    SgBTreeNode *lx = NULL;

    sg_free(n);

    l->parent = p;
    if (p != LAST) {
        if (p->left == n)
            p->left = l;
        else
            p->right = l;
    }

    if (r != LAST) {
        r->parent = l;
        if (l->right != LAST) {
            for (lx = r; lx->left != LAST; lx = lx->left) {}
            lx->left = l->right;
            l->right->parent = lx;
        }
        l->right = r;
    }

    if (lx)
        equilibrate(lx, &r);
    equilibrate(l, &l);
    if (p == LAST)
        *root = l;
    else
        equilibrate(p, root);
}
Exemplo n.º 2
0
void l2capServiceTx(uint16_t conn, uint16_t remChan, sg_buf* data){

    uint8_t hdr[4];
    uint16_t len = sg_length(data);

    putLE16(hdr + 0, len);
    putLE16(hdr + 2, remChan);

    if(sg_add_front(data, hdr, 4, SG_FLAG_MAKE_A_COPY)){

        #if UGLY_SCARY_DEBUGGING_CODE
            uint32_t i;
            uint8_t buf[256];
            sg_copyto(data, buf);
            dbgPrintf("L2CAP TX: ");
            for(i = 0; i < sg_length(data); i++) dbgPrintf(" %02X", buf[i]);
            dbgPrintf("\n");
        #endif

        btAclDataTx(conn, 1, BT_BCAST_NONE, data);
    }
    else{

        sg_free(data);
        free(data);
    }
}
Exemplo n.º 3
0
void l2capServiceTx(uint16_t conn, uint16_t remChan, sg_buf* data){

    uint8_t hdr[4];
    uint16_t len = sg_length(data);

    putLE16(hdr + 0, len);
    putLE16(hdr + 2, remChan);

    if(sg_add_front(data, hdr, 4, SG_FLAG_MAKE_A_COPY)){

   
            uint32_t i;
            uint8_t buf[256];
            sg_copyto(data, buf);
            SIOPrintString("L2CAP TX: ");
            for(i = 0; i < sg_length(data); i++)
            {
                SIOPutHex(buf[i]);
                SIOPutChar(' ');
            }
            SIOPrintString("\r\n");
  

        btAclDataTx(conn, 1, BT_BCAST_NONE, data);
    }
    else{

        sg_free(data);
        free(data);
    }
}
Exemplo n.º 4
0
static void remove_node_right(SgBTreeNode **root, SgBTreeNode *n)
{
    SgBTreeNode *l = n->left, *r = n->right, *p = n->parent;
    SgBTreeNode *rx = NULL;

    sg_free(n);

    r->parent = p;
    if (p != LAST) {
        if (p->left == n)
            p->left = r;
        else
            p->right = r;
    }

    if (l != LAST) {
        l->parent = r;
        if (r->left != LAST) {
            for (rx = l; rx->right != LAST; rx = rx->right) {}
            rx->right = r->left;
            r->left->parent = rx;
        }
        r->left = l;
    }

    if (rx)
        equilibrate(rx, &l);
    equilibrate(r, &r);
    if (p == LAST)
        *root = r;
    else
        equilibrate(p, root);
}
Exemplo n.º 5
0
static void free_btree(SgBTreeNode *n)
{
    if (n != LAST) {
        free_btree(n->left);
        free_btree(n->right);
        sg_free(n);
    }
}
Exemplo n.º 6
0
void l2capServiceTx_ADK(uint16_t conn, uint16_t remChan, const uint8_t* data, uint32_t size){ //send data over L2CAP

    sg_buf* buf = sg_alloc();

    if(!buf) return;
    if(sg_add_back(buf, data, size, SG_FLAG_MAKE_A_COPY)) l2capServiceTx(conn, remChan, buf);
    else sg_free(buf);
}
Exemplo n.º 7
0
void sg_free_list(SgList *ls)
{
    SgList *n;
    for (; ls;) {
        n = ls->next;
        sg_free(ls);
        ls = n;
    }
}
Exemplo n.º 8
0
void check_for_upgrade()
{
    int rc = FAILURE;
    char *new_file_arrived_config = NULL;
    unsigned char reboot = 0;

    RESET_GLOBAL_BUFFER;

    rc = get_config_value_from_persistent_storage(NEW_FILE_KEY, (char*) GLOBAL_BUFFER, MAX_BUFFER_SIZE);
    if(rc == FAILURE)
    {
        sg_sprintf(LOG_GLOBAL_BUFFER, PROSTR("%sConfig not found on persistent storage ... so proceeding with old file"), FILE_UPGRADE);
        error_log(LOG_GLOBAL_BUFFER);

        goto exit;
    }

    new_file_arrived_config = (char*) sg_malloc(MAX_BUFFER_SIZE);
    if(new_file_arrived_config == NULL)
    {
        sg_sprintf(LOG_GLOBAL_BUFFER,
                   PROSTR("%sCould not allocate memory for checking whether new-file arrived ... so proceeding with old file"), FILE_UPGRADE);
        error_log(LOG_GLOBAL_BUFFER);

        goto exit;
    }

    memset(new_file_arrived_config, 0, MAX_BUFFER_SIZE);
    getJsonKeyValueIfPresent((char*)GLOBAL_BUFFER, CONFIG_VALUE_KEY, new_file_arrived_config);

    if(strcmp(new_file_arrived_config, NEW_FILE_ARRIVED) == 0)
    {
        remove_old_executable_binary();
        copy_new_executable_binary_from_temp_location();

        sg_sprintf(LOG_GLOBAL_BUFFER, PROSTR("%sBinary upgraded, restarting to take effect"), FILE_UPGRADE);
        info_log(LOG_GLOBAL_BUFFER);

        reboot = 1;
    }

    delete_config_value_from_persistent_storage(NEW_FILE_KEY);

exit:
    if(new_file_arrived_config)
    {
        sg_free(new_file_arrived_config);
    }

    if(reboot == 1)
    {
        exitApp();
    }
}
Exemplo n.º 9
0
static void remove_leaf(SgBTreeNode **root, SgBTreeNode *n)
{
    SgBTreeNode *p = n->parent;

    sg_free(n);

    if (p == LAST) {
        *root = LAST;
        return;
    }
    if (p->left == n)
        p->left = LAST;
    else
        p->right = LAST;
    equilibrate(p, root);
}
Exemplo n.º 10
0
static void l2capSendControlRawBuf(uint16_t conn, const uint8_t* data, uint16_t len){

    sg_buf* buf;

    buf = sg_alloc();
    if(buf){

        if(sg_add_back(buf, data, len, SG_FLAG_MAKE_A_COPY)){

            l2capServiceTx(conn, 1, buf);
        }
        else{

            sg_free(buf);
            free(buf);
        }
    }
}
Exemplo n.º 11
0
static void btRfcommSend(uint16_t conn, uint16_t remChan, const uint8_t* data, uint16_t sz){

    int i;

    #if UGLY_SCARY_DEBUGGING_CODE
        dbgPrintf("Sending RFCOMM packet:");
        for(i = 0; i < sz; i++) dbgPrintf(" %02X", data[i]);
        dbgPrintf("\n\n");
    #endif

    sg_buf* buf = sg_alloc();
    if(!buf) return;
    if(sg_add_front(buf, data, sz, SG_FLAG_MAKE_A_COPY)){

        l2capServiceTx(conn, remChan, buf);
        return;
    }
    sg_free(buf);
    free(buf);
}
Exemplo n.º 12
0
void sg_display_btree(SgBTreeNode *n)
{
    SgSize ml, ls, i, j;
    SgChar *buff;

    if (n == LAST)
        return;
    ml = n->mlevel;
    ls = (2 << ml) / 2;
    buff = sg_alloc(ls * ml);

    for (i = 0; i < ml; i++) {
        for (j = 0; j < (ls - 1); j++) {
            buff[i*ls+j] = ' ';
        }
        buff[i*ls+j] = 0;
    }

    fill_buff(n, buff, 0, 0, ml, ls);
    for (; ml;) {
        printf("%s\n", &buff[--ml*ls]);
    }
    sg_free(buff);
}
Exemplo n.º 13
0
static void btRfcommSend(uint16_t conn, uint16_t remChan, const uint8_t* data, uint16_t sz){

    int i;


        SIOPrintString("Sending RFCOMM packet:");
        for(i = 0; i < sz; i++)
        {
            SIOPutHex(data[i]);
            SIOPutChar(' ');
        }
        SIOPrintString("\r\n");


    sg_buf* buf = sg_alloc();
    if(!buf) return;
    if(sg_add_front(buf, data, sz, SG_FLAG_MAKE_A_COPY)){

        l2capServiceTx(conn, remChan, buf);
        return;
    }
    sg_free(buf);
    free(buf);
}
Exemplo n.º 14
0
void sg_free_btree(SgBTree *h)
{
    free_btree(h->root);
    sg_free(h);
}
Exemplo n.º 15
0
void sg_free_vector(SgVector *v)
{
    sg_free(v->head);
    sg_free(v);
}
Exemplo n.º 16
0
static ssize_t sg_write(struct file *filp, const char *buf,
                        size_t count, loff_t *ppos)
{
    unsigned long	  flags;
    struct inode         *inode = filp->f_dentry->d_inode;
    int			  bsize,size,amt,i;
    unsigned char	  cmnd[MAX_COMMAND_SIZE];
    kdev_t		  devt = inode->i_rdev;
    int			  dev = MINOR(devt);
    struct scsi_generic   * device=&scsi_generics[dev];
    int			  input_size;
    unsigned char	  opcode;
    Scsi_Cmnd		* SCpnt;

    /*
     * If we are in the middle of error recovery, don't let anyone
     * else try and use this device.  Also, if error recovery fails, it
     * may try and take the device offline, in which case all further
     * access to the device is prohibited.
     */
    if( !scsi_block_when_processing_errors(scsi_generics[dev].device) )
    {
        return -ENXIO;
    }

    if (ppos != &filp->f_pos) {
        /* FIXME: Hmm.  Seek to the right place, or fail?  */
    }

    if ((i=verify_area(VERIFY_READ,buf,count)))
        return i;
    /*
     * The minimum scsi command length is 6 bytes.  If we get anything
     * less than this, it is clearly bogus.
     */
    if (count<(sizeof(struct sg_header) + 6))
        return -EIO;

    /*
     * If we still have a result pending from a previous command,
     * wait until the result has been read by the user before sending
     * another command.
     */
    while(device->pending)
    {
        if (filp->f_flags & O_NONBLOCK)
            return -EAGAIN;
#ifdef DEBUG
        printk("sg_write: sleeping on pending request\n");
#endif
        interruptible_sleep_on(&device->write_wait);
        if (signal_pending(current))
            return -ERESTARTSYS;
    }

    /*
     * Mark the device flags for the new state.
     */
    device->pending=1;
    device->complete=0;
    copy_from_user(&device->header,buf,sizeof(struct sg_header));

    device->header.pack_len=count;
    buf+=sizeof(struct sg_header);

    /*
     * Now we need to grab the command itself from the user's buffer.
     */
    get_user(opcode, buf);
    size=COMMAND_SIZE(opcode);
    if (opcode >= 0xc0 && device->header.twelve_byte) size = 12;

    /*
     * Determine buffer size.
     */
    input_size = device->header.pack_len - size;
    if( input_size > device->header.reply_len)
    {
        bsize = input_size;
    } else {
        bsize = device->header.reply_len;
    }

    /*
     * Don't include the command header itself in the size.
     */
    bsize-=sizeof(struct sg_header);
    input_size-=sizeof(struct sg_header);

    /*
     * Verify that the user has actually passed enough bytes for this command.
     */
    if( input_size < 0 )
    {
        device->pending=0;
        wake_up( &device->write_wait );
        return -EIO;
    }

    /*
     * Allocate a buffer that is large enough to hold the data
     * that has been requested.  Round up to an even number of sectors,
     * since scsi_malloc allocates in chunks of 512 bytes.
     */
    amt=bsize;
    if (!bsize)
        bsize++;
    bsize=(bsize+511) & ~511;

    /*
     * If we cannot allocate the buffer, report an error.
     */
    if ((bsize<0) || !(device->buff=sg_malloc(device->buff_len=bsize)))
    {
        device->pending=0;
        wake_up(&device->write_wait);
        return -ENOMEM;
    }

#ifdef DEBUG
    printk("allocating device\n");
#endif

    /*
     * Grab a device pointer for the device we want to talk to.  If we
     * don't want to block, just return with the appropriate message.
     */
    if (!(SCpnt=scsi_allocate_device(NULL,device->device, !(filp->f_flags & O_NONBLOCK))))
    {
        device->pending=0;
        wake_up(&device->write_wait);
        sg_free(device->buff,device->buff_len);
        device->buff = NULL;
        return -EAGAIN;
    }
#ifdef DEBUG
    printk("device allocated\n");
#endif

    SCpnt->request.rq_dev = devt;
    SCpnt->request.rq_status = RQ_ACTIVE;
    SCpnt->sense_buffer[0]=0;
    SCpnt->cmd_len = size;

    /*
     * Now copy the SCSI command from the user's address space.
     */
    copy_from_user(cmnd,buf,size);
    buf+=size;

    /*
     * If we are writing data, copy the data we are writing.  The pack_len
     * field also includes the length of the header and the command,
     * so we need to subtract these off.
     */
    if (input_size > 0) copy_from_user(device->buff, buf, input_size);

    /*
     * Set the LUN field in the command structure.
     */
    cmnd[1]= (cmnd[1] & 0x1f) | (device->device->lun<<5);

#ifdef DEBUG
    printk("do cmd\n");
#endif

    /*
     * Now pass the actual command down to the low-level driver.  We
     * do not do any more here - when the interrupt arrives, we will
     * then do the post-processing.
     */
    spin_lock_irqsave(&io_request_lock, flags);
    scsi_do_cmd (SCpnt,(void *) cmnd,
                 (void *) device->buff,amt,
                 sg_command_done,device->timeout,SG_DEFAULT_RETRIES);
    spin_unlock_irqrestore(&io_request_lock, flags);

#ifdef DEBUG
    printk("done cmd\n");
#endif

    return count;
}
Exemplo n.º 17
0
/*
 * Read back the results of a previous command.  We use the pending and
 * complete semaphores to tell us whether the buffer is available for us
 * and whether the command is actually done.
 */
static ssize_t sg_read(struct file *filp, char *buf,
                       size_t count, loff_t *ppos)
{
    struct inode *inode = filp->f_dentry->d_inode;
    int dev=MINOR(inode->i_rdev);
    int i;
    struct scsi_generic *device=&scsi_generics[dev];

    /*
     * If we are in the middle of error recovery, don't let anyone
     * else try and use this device.  Also, if error recovery fails, it
     * may try and take the device offline, in which case all further
     * access to the device is prohibited.
     */
    if( !scsi_block_when_processing_errors(scsi_generics[dev].device) )
    {
        return -ENXIO;
    }

    if (ppos != &filp->f_pos) {
        /* FIXME: Hmm.  Seek to the right place, or fail?  */
    }

    if ((i=verify_area(VERIFY_WRITE,buf,count)))
        return i;

    /*
     * Wait until the command is actually done.
     */
    while(!device->pending || !device->complete)
    {
        if (filp->f_flags & O_NONBLOCK)
        {
            return -EAGAIN;
        }
        interruptible_sleep_on(&device->read_wait);
        if (signal_pending(current))
        {
            return -ERESTARTSYS;
        }
    }

    /*
     * Now copy the result back to the user buffer.
     */
    device->header.pack_len=device->header.reply_len;

    if (count>=sizeof(struct sg_header))
    {
        copy_to_user(buf,&device->header,sizeof(struct sg_header));
        buf+=sizeof(struct sg_header);
        if (count>device->header.pack_len)
            count=device->header.pack_len;
        if (count > sizeof(struct sg_header)) {
            copy_to_user(buf,device->buff,count-sizeof(struct sg_header));
        }
    }
    else
        count= device->header.result==0 ? 0 : -EIO;

    /*
     * Clean up, and release the device so that we can send another
     * command.
     */
    sg_free(device->buff,device->buff_len);
    device->buff = NULL;
    device->pending=0;
    wake_up(&device->write_wait);
    return count;
}
Exemplo n.º 18
0
static int sg_open(struct inode * inode, struct file * filp)
{
    int dev=MINOR(inode->i_rdev);
    int flags=filp->f_flags;
    if (dev>=sg_template.dev_max || !scsi_generics[dev].device)
        return -ENXIO;

    if( !scsi_block_when_processing_errors(scsi_generics[dev].device) )
    {
        return -ENXIO;
    }

    if (O_RDWR!=(flags & O_ACCMODE))
        return -EACCES;

    /*
     * If we want exclusive access, then wait until the device is not
     * busy, and then set the flag to prevent anyone else from using it.
     */
    if (flags & O_EXCL)
    {
        while(scsi_generics[dev].users)
        {
            if (flags & O_NONBLOCK)
                return -EBUSY;
            interruptible_sleep_on(&scsi_generics[dev].generic_wait);
            if (signal_pending(current))
                return -ERESTARTSYS;
        }
        scsi_generics[dev].exclude=1;
    }
    else
        /*
         * Wait until nobody has an exclusive open on
         * this device.
         */
        while(scsi_generics[dev].exclude)
        {
            if (flags & O_NONBLOCK)
                return -EBUSY;
            interruptible_sleep_on(&scsi_generics[dev].generic_wait);
            if (signal_pending(current))
                return -ERESTARTSYS;
        }

    /*
     * OK, we should have grabbed the device.  Mark the thing so
     * that other processes know that we have it, and initialize the
     * state variables to known values.
     */
    if (!scsi_generics[dev].users
            && scsi_generics[dev].pending
            && scsi_generics[dev].complete)
    {
        if (scsi_generics[dev].buff != NULL)
            sg_free(scsi_generics[dev].buff,scsi_generics[dev].buff_len);
        scsi_generics[dev].buff=NULL;
        scsi_generics[dev].pending=0;
    }
    if (!scsi_generics[dev].users)
        scsi_generics[dev].timeout=SG_DEFAULT_TIMEOUT;
    if (scsi_generics[dev].device->host->hostt->module)
        __MOD_INC_USE_COUNT(scsi_generics[dev].device->host->hostt->module);
    if (sg_template.module)
        __MOD_INC_USE_COUNT(sg_template.module);
    scsi_generics[dev].users++;
    return 0;
}