private void ParseDot(Packet mPack, DotTypes type) { int timeadd = mPack.GetByte(); mTime += timeadd; int force = mPack.GetShort(); int x = mPack.GetShort(); int y = mPack.GetShort(); int fx = mPack.GetByte(); int fy = mPack.GetByte(); int tx = mPack.GetByte(); int ty = mPack.GetByte(); int twist = mPack.GetShort(); PenController.onReceiveDot(new DotReceivedEventArgs(MakeDot(PenMaxForce, mCurOwner, mCurSection, mCurNote, mCurPage, mTime, x, y, fx, fy, force, type, mPenTipColor))); }
public PenClientParserV2(PenController penClient) { this.PenController = penClient; }
public void ParsePacket(Packet packet) { Cmd cmd = (Cmd)packet.Cmd; //Debug.Write("Cmd : {0}", cmd.ToString()); switch (cmd) { case Cmd.VERSION_RESPONSE: { DeviceName = packet.GetString(16); FirmwareVersion = packet.GetString(16); ProtocolVersion = packet.GetString(8); SubName = packet.GetString(16); DeviceType = packet.GetShort(); MaxForce = -1; MacAddress = BitConverter.ToString(packet.GetBytes(6)).Replace("-", ""); IsUploading = false; ReqPenStatus(); } break; #region event case Cmd.SHUTDOWN_EVENT: { byte reason = packet.GetByte(); Debug.Write(" => SHUTDOWN_EVENT : {0}", reason.ToString()); } break; case Cmd.LOW_BATTERY_EVENT: { int battery = (int)(packet.GetByte() & 0xff); PenController.onReceiveBatteryAlarm(new BatteryAlarmReceivedEventArgs(battery)); } break; case Cmd.ONLINE_PEN_UPDOWN_EVENT: case Cmd.ONLINE_PEN_DOT_EVENT: case Cmd.ONLINE_PAPER_INFO_EVENT: { ParseDotPacket(cmd, packet); } break; #endregion #region setting response case Cmd.SETTING_INFO_RESPONSE: { // 비밀번호 사용 여부 bool lockyn = packet.GetByteToInt() == 1; // 비밀번호 입력 최대 시도 횟수 int pwdMaxRetryCount = packet.GetByteToInt(); // 비밀번호 입력 시도 횟수 int pwdRetryCount = packet.GetByteToInt(); // 1970년 1월 1일부터 millisecond tick long time = packet.GetLong(); // 사용하지 않을때 자동으로 전원이 종료되는 시간 (단위:분) short autoPowerOffTime = packet.GetShort(); // 최대 필압 short maxForce = packet.GetShort(); // 현재 메모리 사용량 int usedStorage = packet.GetByteToInt(); // 펜의 뚜껑을 닫아서 펜의 전원을 차단하는 기능 사용 여부 bool penCapOff = packet.GetByteToInt() == 1; // 전원이 꺼진 펜에 필기를 시작하면 자동으로 펜의 켜지는 옵션 사용 여부 bool autoPowerON = packet.GetByteToInt() == 1; // 사운드 사용여부 bool beep = packet.GetByteToInt() == 1; // 호버기능 사용여부 bool hover = packet.GetByteToInt() == 1; // 남은 배터리 수치 int batteryLeft = packet.GetByteToInt(); // 오프라인 데이터 저장 기능 사용 여부 bool useOffline = packet.GetByteToInt() == 1; // 필압 단계 설정 (0~4) 0이 가장 민감 short fsrStep = (short)packet.GetByteToInt(); // 최초 연결시 if (MaxForce == -1) { MaxForce = maxForce; var connectedEventArgs = new ConnectedEventArgs(); PenController.onConnected(new ConnectedEventArgs(MacAddress, DeviceName, FirmwareVersion, ProtocolVersion, SubName, MaxForce)); PenMaxForce = MaxForce; if (lockyn) { // TODO 여기서 부터 시작 PenController.onPenPasswordRequest(new PasswordRequestedEventArgs(pwdRetryCount, pwdMaxRetryCount)); } else { PenController.onPenAuthenticated(); } } else { PenController.onReceivePenStatus(new PenStatusReceivedEventArgs(lockyn, pwdMaxRetryCount, pwdRetryCount, time, autoPowerOffTime, MaxForce, batteryLeft, usedStorage, useOffline, autoPowerON, penCapOff, hover, beep, fsrStep)); } } break; case Cmd.SETTING_CHANGE_RESPONSE: { int inttype = packet.GetByteToInt(); SettingType stype = (SettingType)inttype; bool result = packet.Result == 0x00; switch (stype) { case SettingType.AutoPowerOffTime: PenController.onPenAutoShutdownTimeSetupResponse(new SimpleResultEventArgs(result)); break; case SettingType.AutoPowerOn: PenController.onPenAutoPowerOnSetupResponse(new SimpleResultEventArgs(result)); break; case SettingType.Beep: PenController.onPenBeepSetupResponse(new SimpleResultEventArgs(result)); break; case SettingType.Hover: PenController.onPenHoverSetupResponse(new SimpleResultEventArgs(result)); break; case SettingType.LedColor: PenController.onPenColorSetupResponse(new SimpleResultEventArgs(result)); break; case SettingType.OfflineData: PenController.onPenOfflineDataSetupResponse(new SimpleResultEventArgs(result)); break; case SettingType.PenCapOff: PenController.onPenCapPowerOnOffSetupResponse(new SimpleResultEventArgs(result)); break; case SettingType.Sensitivity: PenController.onPenSensitivitySetupResponse(new SimpleResultEventArgs(result)); break; case SettingType.Timestamp: PenController.onPenTimestampSetupResponse(new SimpleResultEventArgs(result)); break; } } break; #endregion #region password response case Cmd.PASSWORD_RESPONSE: { int status = packet.GetByteToInt(); int cntRetry = packet.GetByteToInt(); int cntMax = packet.GetByteToInt(); if (status == 0) { PenController.onPenPasswordRequest(new PasswordRequestedEventArgs(cntRetry, cntMax)); } else { PenController.onPenAuthenticated(); } } break; case Cmd.PASSWORD_CHANGE_RESPONSE: { int cntRetry = packet.GetByteToInt(); int cntMax = packet.GetByteToInt(); PenController.onPenPasswordSetupResponse(new SimpleResultEventArgs(packet.Result == 0x00)); } break; #endregion #region offline response case Cmd.OFFLINE_NOTE_LIST_RESPONSE: { short length = packet.GetShort(); List <OfflineDataInfo> result = new List <OfflineDataInfo>(); for (int i = 0; i < length; i++) { byte[] rb = packet.GetBytes(4); int section = (int)(rb[3] & 0xFF); int owner = ByteConverter.ByteToInt(new byte[] { rb[0], rb[1], rb[2], (byte)0x00 }); int note = packet.GetInt(); result.Add(new OfflineDataInfo(section, owner, note)); } PenController.onReceiveOfflineDataList(new OfflineDataListReceivedEventArgs(result.ToArray())); } break; case Cmd.OFFLINE_PAGE_LIST_RESPONSE: { byte[] rb = packet.GetBytes(4); int section = (int)(rb[3] & 0xFF); int owner = ByteConverter.ByteToInt(new byte[] { rb[0], rb[1], rb[2], (byte)0x00 }); int note = packet.GetInt(); short length = packet.GetShort(); int[] pages = new int[length]; for (int i = 0; i < length; i++) { pages[i] = packet.GetInt(); } OfflineDataInfo info = new OfflineDataInfo(section, owner, note); PenController.onReceiveOfflineDataList(new OfflineDataListReceivedEventArgs(info)); } break; case Cmd.OFFLINE_DATA_RESPONSE: { mTotalOfflineStroke = packet.GetInt(); mReceivedOfflineStroke = 0; mTotalOfflineDataSize = packet.GetInt(); bool isCompressed = packet.GetByte() == 1; PenController.onStartOfflineDownload(); } break; case Cmd.OFFLINE_PACKET_REQUEST: { #region offline data parsing List <Stroke> result = new List <Stroke>(); short packetId = packet.GetShort(); bool isCompressed = packet.GetByte() == 1; short sizeBefore = packet.GetShort(); short sizeAfter = packet.GetShort(); short location = (short)(packet.GetByte() & 0xFF); byte[] rb = packet.GetBytes(4); int section = (int)(rb[3] & 0xFF); int owner = ByteConverter.ByteToInt(new byte[] { rb[0], rb[1], rb[2], (byte)0x00 }); int note = packet.GetInt(); short strCount = packet.GetShort(); mReceivedOfflineStroke += strCount; Debug.WriteLine(" packetId : {0}, isCompressed : {1}, sizeBefore : {2}, sizeAfter : {3}, size : {4}", packetId, isCompressed, sizeBefore, sizeAfter, packet.Data.Length - 18); if (sizeAfter != (packet.Data.Length - 18)) { SendOfflinePacketResponse(packetId, false); return; } byte[] oData = packet.GetBytes(sizeAfter); GZipStream gzipStream = new GZipStream(new System.IO.MemoryStream(oData), CompressionLevel.Fastest); byte[] strData = Ionic.Zlib.ZlibStream.UncompressBuffer(oData); if (strData.Length != sizeBefore) { SendOfflinePacketResponse(packetId, false); return; } ByteUtil butil = new ByteUtil(strData); for (int i = 0; i < strCount; i++) { int pageId = butil.GetInt(); long timeStart = butil.GetLong(); long timeEnd = butil.GetLong(); int penTipType = (int)(butil.GetByte() & 0xFF); int color = butil.GetInt(); short dotCount = butil.GetShort(); long time = timeStart; //System.Console.WriteLine( "pageId : {0}, timeStart : {1}, timeEnd : {2}, penTipType : {3}, color : {4}, dotCount : {5}, time : {6},", pageId, timeStart, timeEnd, penTipType, color, dotCount, time ); Stroke stroke = new Stroke(section, owner, note, pageId); for (int j = 0; j < dotCount; j++) { byte dotChecksum = butil.GetChecksum(15); int timeadd = butil.GetByte(); time += timeadd; int force = butil.GetShort(); int x = butil.GetShort(); int y = butil.GetShort(); int fx = butil.GetByte(); int fy = butil.GetByte(); int tx = butil.GetByte(); int ty = butil.GetByte(); int twist = butil.GetShort(); short reserved = butil.GetShort(); byte checksum = butil.GetByte(); //System.Console.WriteLine( "x : {0}, y : {1}, force : {2}, checksum : {3}, dotChecksum : {4}", tx, ty, twist, checksum, dotChecksum ); if (dotChecksum != checksum) { SendOfflinePacketResponse(packetId, false); result.Clear(); return; } DotTypes dotType; if (j == 0) { dotType = DotTypes.PEN_DOWN; } else if (j == dotCount - 1) { dotType = DotTypes.PEN_UP; } else { dotType = DotTypes.PEN_MOVE; } stroke.Add(MakeDot(PenMaxForce, owner, section, note, pageId, time, x, y, fx, fy, force, dotType, color)); } result.Add(stroke); } SendOfflinePacketResponse(packetId); PenController.onReceiveOfflineStrokes(new OfflineStrokeReceivedEventArgs(mTotalOfflineStroke, mReceivedOfflineStroke, result.ToArray())); if (location == 2) { PenController.onFinishedOfflineDownload(new SimpleResultEventArgs(true)); } #endregion } break; case Cmd.OFFLINE_DATA_DELETE_RESPONSE: { PenController.onRemovedOfflineData(new SimpleResultEventArgs(packet.Result == 0x00)); } break; #endregion #region firmware response case Cmd.FIRMWARE_UPLOAD_RESPONSE: { if (packet.Result != 0 || packet.GetByteToInt() != 0) { IsUploading = false; PenController.onReceiveFirmwareUpdateResult(new SimpleResultEventArgs(false)); } } break; case Cmd.FIRMWARE_PACKET_REQUEST: { int status = packet.GetByteToInt(); int offset = packet.GetInt(); ResponseChunkRequest(offset, status != 3); } break; #endregion case Cmd.ONLINE_DATA_RESPONSE: break; default: break; } }