Example #1
0
 private void btnWriteValue_Click(object sender, EventArgs e)
 {
     formDataAccess.WaitOne();
     if (tbValue.Text == null || tbValue.Text == string.Empty)
     {
         msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Warning, "A Value Must Be Entered To Perform A Write\n");
     }
     else
     {
         string outStr = string.Empty;
         ValueDisplay inValueDisplay = dataAttr.ValueDisplay;
         ValueDisplay outValueDisplay = ValueDisplay.Hex;
         if (lastValueDisplaySet)
             inValueDisplay = lastValueDisplay;
         if (devUtils.ConvertDisplayTypes(inValueDisplay, tbValue.Text, ref outValueDisplay, ref outStr, true))
         {
             string str = devUtils.HexStr2UserDefinedStr(outStr, SharedAppObjs.StringType.HEX);
             if (str == null || str == string.Empty)
             {
                 string msg = "Value Data Cannot Be Converted To Hex For Write Command\n";
                 msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Warning, msg);
             }
             else
             {
                 devForm.threadMgr.rspDataIn.ExtCmdStatus.ExtCmdStatusCallback = new ExtCmdStatus.ExtCmdStatusDelegate(ExtCmdStatus);
                 devForm.threadMgr.rspDataIn.AttErrorRsp.AttErrorRspCallback = new AttErrorRsp.AttErrorRspDelegate(AttErrorRsp);
                 devForm.threadMgr.rspDataIn.AttPrepareWriteRsp.AttPrepareWriteRspCallback = new AttPrepareWriteRsp.AttPrepareWriteRspDelegate(AttPrepareWriteRsp);
                 devForm.threadMgr.rspDataIn.AttExecuteWriteRsp.AttExecuteWriteRspCallback = new AttExecuteWriteRsp.AttExecuteWriteRspDelegate(AttExecuteWriteRsp);
                 HCICmds.GATTCmds.GATT_WriteLongCharValue writeLongCharValue = new HCICmds.GATTCmds.GATT_WriteLongCharValue();
                 writeLongCharValue.connHandle = dataAttr.ConnHandle;
                 writeLongCharValue.handle = dataAttr.Handle;
                 writeLongCharValue.value = str;
                 gattWriteDataAttr = dataAttr;
                 gattWriteDataAttr.Value = str;
                 int length1 = AttrData.writeLimits.MaxPacketSize >= AttrData.writeLimits.MaxNumPreparedWrites * 18 ? AttrData.writeLimits.MaxNumPreparedWrites * 18 : AttrData.writeLimits.MaxPacketSize;
                 byte[] numArray = devUtils.String2Bytes_LSBMSB(str, (byte)16);
                 if (numArray == null)
                 {
                     sendCmds.DisplayInvalidValue(writeLongCharValue.value);
                 }
                 else
                 {
                     int length2 = numArray.Length;
                     int sourceIndex = 0;
                     while (sourceIndex < numArray.Length)
                     {
                         byte[] valueData;
                         if (length2 > length1)
                         {
                             valueData = new byte[length1];
                             Array.Copy((Array)numArray, sourceIndex, (Array)valueData, 0, length1);
                         }
                         else
                         {
                             valueData = new byte[length2];
                             Array.Copy((Array)numArray, sourceIndex, (Array)valueData, 0, length2);
                         }
                         writeLongCharValue.value = string.Empty;
                         writeLongCharValue.offset = (ushort)sourceIndex;
                         if (sendCmds.SendGATT(writeLongCharValue, valueData, new SendCmds.SendCmdResult(SendCmdResult)))
                         {
                             Enabled = false;
                             int length3 = valueData.Length;
                             length2 -= valueData.Length;
                             sourceIndex += length3;
                         }
                         else
                         {
                             string msg = "GATT_WriteLongCharValue Command Failed\n";
                             if (sourceIndex > 0)
                                 msg = msg + "Multi-Part Write Sequenece Error\n" + "All Requested Data May Not Have Been Written To The Device\n";
                             if (DisplayMsgCallback != null)
                                 DisplayMsgCallback(SharedAppObjs.MsgType.Error, msg);
                             msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                             ClearRspDelegates();
                             break;
                         }
                     }
                 }
             }
         }
     }
     formDataAccess.ReleaseMutex();
 }