Skip to content

Commit e84c311

Browse files
committed
tommos ok
1 parent b7562ce commit e84c311

File tree

9 files changed

+5163
-313
lines changed

9 files changed

+5163
-313
lines changed

src/main/docan/toomoss/inc/blockconcurrentqueue.h

Lines changed: 582 additions & 0 deletions
Large diffs are not rendered by default.

src/main/docan/toomoss/inc/concurrentqueue.h

Lines changed: 3747 additions & 0 deletions
Large diffs are not rendered by default.

src/main/docan/toomoss/inc/lightweightsemaphore.h

Lines changed: 427 additions & 0 deletions
Large diffs are not rendered by default.

src/main/docan/toomoss/index.ts

Lines changed: 105 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ interface CANFrame {
2424
ts: number
2525
}
2626

27+
let txCnt = 0
28+
const pendingBaseCmds = new Map<
29+
number,
30+
{
31+
resolve: (value: number) => void
32+
reject: (reason: CanError) => void
33+
msgType: CanMsgType
34+
id: number
35+
data: Buffer
36+
extra?: { database?: string; name?: string }
37+
}
38+
>()
39+
2740
export class TOOMOSS_CAN extends CanBase {
2841
// 添加静态设备管理Map
2942
static deviceHandles = new Map<number, {
@@ -38,23 +51,16 @@ export class TOOMOSS_CAN extends CanBase {
3851
deviceIndex: number
3952
closed = false
4053
cnt = 0
54+
4155
id: string
4256
log: CanLOG
4357
canConfig: any
4458
canfdConfig: any
4559
startTime = getTsUs()
4660
tsOffset: number | undefined
61+
4762
private readAbort = new AbortController()
48-
pendingBaseCmds = new Map<
49-
string,
50-
{
51-
resolve: (value: number) => void
52-
reject: (reason: CanError) => void
53-
msgType: CanMsgType
54-
data: Buffer
55-
extra?: { database?: string; name?: string }
56-
}[]
57-
>()
63+
5864

5965
rejectBaseMap = new Map<
6066
number,
@@ -64,7 +70,7 @@ export class TOOMOSS_CAN extends CanBase {
6470
}
6571
>()
6672

67-
constructor(info: CanBaseInfo,enableRes=false) {
73+
constructor(info: CanBaseInfo, enableRes = false) {
6874
super()
6975
this.id = info.id
7076
this.info = info
@@ -154,7 +160,7 @@ export class TOOMOSS_CAN extends CanBase {
154160
this.canfdConfig.Mode = 0 // 正常模式
155161
this.canfdConfig.ISOCRCEnable = 1
156162
this.canfdConfig.RetrySend = 0
157-
this.canfdConfig.ResEnable = enableRes?1:0
163+
this.canfdConfig.ResEnable = enableRes ? 1 : 0
158164
// 仲裁域波特率配置
159165
this.canfdConfig.NBT_BRP = info.bitrate.preScaler
160166
this.canfdConfig.NBT_SEG1 = info.bitrate.timeSeg1
@@ -174,7 +180,7 @@ export class TOOMOSS_CAN extends CanBase {
174180
TOOMOSS.CANFD_ResetStartTime(this.handle, this.deviceIndex)
175181

176182

177-
// // 启动CANFD
183+
// 启动CANFD
178184
ret = TOOMOSS.CANFD_StartGetMsg(this.handle, this.deviceIndex)
179185
if (ret != 0) {
180186
throw new Error('Start CANFD failed')
@@ -187,7 +193,7 @@ export class TOOMOSS_CAN extends CanBase {
187193
this.canConfig.CAN_SJW = info.bitrate.sjw
188194
this.canConfig.CAN_BS1 = info.bitrate.timeSeg1
189195
this.canConfig.CAN_BS2 = info.bitrate.timeSeg2
190-
this.canConfig.CAN_Mode = enableRes?(0x80|0):0 // 正常模式
196+
this.canConfig.CAN_Mode = enableRes ? (0x80 | 0) : 0 // 正常模式
191197
this.canConfig.CAN_ABOM = 0 // 自动离线恢复
192198
this.canConfig.CAN_NART = 1 // 自动重传
193199
this.canConfig.CAN_RFLM = 0 // 接收FIFO锁定模式
@@ -207,24 +213,64 @@ export class TOOMOSS_CAN extends CanBase {
207213
TOOMOSS.CAN_ResetStartTime(this.handle, this.deviceIndex)
208214
}
209215

216+
// 绑定回调函数到实例
217+
210218

211-
// 启动消息接收
219+
// 初始化 TSFN
212220
TOOMOSS.CreateTSFN(
213221
this.handle,
214222
this.deviceIndex,
215-
info.canfd,
223+
this.info.canfd,
216224
this.id,
217225
this.callback.bind(this),
218-
this.id + 'error',
219-
this.callbackError.bind(this)
226+
'error-' + this.id,
227+
this.callbackError.bind(this),
228+
'tx-' + this.id,
229+
this.txCallback.bind(this)
230+
220231
)
221232
}
233+
txCallback(obj: any) {
234+
const { id, timestamp, result } = obj
235+
236+
237+
238+
// 更新时间戳偏移
239+
if (this.tsOffset == undefined) {
240+
this.tsOffset = timestamp * 10 - (getTsUs() - this.startTime)
241+
}
242+
const ts = timestamp * 10 - this.tsOffset
243+
244+
// 从待发送队列中找到对应的消息
245+
const cmdId = Number(id)
246+
247+
const pendingCmds = pendingBaseCmds.get(cmdId)
248+
249+
250+
if (pendingCmds) {
251+
252+
const message: CanMessage = {
253+
device: this.info.name,
254+
dir: 'OUT',
255+
id: pendingCmds.id,
256+
data: pendingCmds.data,
257+
ts: ts,
258+
msgType: pendingCmds.msgType,
259+
database: pendingCmds.extra?.database,
260+
name: pendingCmds.extra?.name
261+
}
262+
this.log.canBase(message)
263+
pendingCmds.resolve(ts)
264+
pendingBaseCmds.delete(cmdId)
222265

266+
}
267+
}
223268
callback(msg: any) {
224269
if (msg.id == 0xffffffff) {
225270
return
226271
}
227272

273+
228274
const frame: CANFrame = {
229275
canId: msg.ID & 0x1fffffff,
230276
msgType: {
@@ -236,7 +282,7 @@ export class TOOMOSS_CAN extends CanBase {
236282
data: msg.Data,
237283
ts: ((msg.TimeStampHigh << 32) | msg.TimeStamp) * 10
238284
}
239-
285+
240286
if (this.info.canfd) {
241287
// CANFD消息处理
242288
frame.msgType.brs = (msg.Flags & 0x01) ? true : false
@@ -257,9 +303,11 @@ export class TOOMOSS_CAN extends CanBase {
257303
}
258304

259305
callbackError(err: any) {
306+
console.log('callbackError', err)
260307
this.log.error(getTsUs() - this.startTime, 'bus error')
261308
this.close(true)
262309
}
310+
263311
setOption(cmd: string, val: any): void {
264312
null
265313
}
@@ -268,7 +316,7 @@ export class TOOMOSS_CAN extends CanBase {
268316
this.tsOffset = frame.ts - (getTsUs() - this.startTime)
269317
}
270318
const ts = frame.ts - this.tsOffset
271-
319+
272320
const cmdId = this.getReadBaseId(frame.canId, frame.msgType)
273321
const message: CanMessage = {
274322
device: this.info.name,
@@ -319,18 +367,18 @@ export class TOOMOSS_CAN extends CanBase {
319367

320368
close(isReset = false, msg?: string) {
321369
this.readAbort.abort()
322-
370+
323371
for (const [key, value] of this.rejectBaseMap) {
324372
value.reject(new CanError(CAN_ERROR_ID.CAN_BUS_CLOSED, value.msgType))
325373
}
374+
326375
this.rejectBaseMap.clear()
327376

328-
for (const [key, vals] of this.pendingBaseCmds) {
329-
for (const val of vals) {
330-
val.reject(new CanError(CAN_ERROR_ID.CAN_BUS_CLOSED, val.msgType))
331-
}
377+
for (const [key, val] of pendingBaseCmds) {
378+
val.reject(new CanError(CAN_ERROR_ID.CAN_BUS_CLOSED, val.msgType))
332379
}
333-
this.pendingBaseCmds.clear()
380+
pendingBaseCmds.clear()
381+
334382

335383
if (isReset) {
336384
if (this.info.canfd) {
@@ -346,7 +394,7 @@ export class TOOMOSS_CAN extends CanBase {
346394
}
347395
return
348396
} else {
349-
397+
350398
this.closed = true
351399
this.log.close()
352400
TOOMOSS.FreeTSFN(this.id)
@@ -376,42 +424,37 @@ export class TOOMOSS_CAN extends CanBase {
376424

377425
writeBase(id: number, msgType: CanMsgType, data: Buffer, extra?: { database?: string; name?: string }): Promise<number> {
378426
return new Promise((resolve, reject) => {
379-
const maxLen = msgType.canfd ? 64 : 8
380-
if (data.length > maxLen) {
381-
reject(new CanError(CAN_ERROR_ID.CAN_PARAM_ERROR, msgType, data))
382-
return
383-
}
384-
console.log('send start')
385-
TOOMOSS.SendCANMsg(this.handle, this.deviceIndex, this.info.canfd, {
386-
ID: id | (msgType.idType == CAN_ID_TYPE.EXTENDED ? 0x80000000 : 0) | (msgType.remote ? 0x40000000 : 0),
387-
RemoteFlag: msgType.remote ? 1 : 0,
388-
ExternFlag: msgType.idType == CAN_ID_TYPE.EXTENDED ? 1 : 0,
389-
DataLen: data.length,
390-
Data: data,
391-
DLC: data.length,
392-
Flags: (msgType.brs ? 0x01 : 0) | (msgType.canfd ? 0x04 : 0)
393-
}).then((timestamp: number) => {
394-
console.log('send ok')
395-
if (this.tsOffset == undefined) {
396-
this.tsOffset = timestamp*10 - (getTsUs() - this.startTime)
397-
}
398-
const ts=timestamp*10-this.tsOffset
399-
const message: CanMessage = {
400-
device: this.info.name,
401-
dir: 'OUT',
402-
id: id,
403-
data: data,
404-
ts: ts, // 转换为微秒
405-
msgType: msgType,
406-
database: extra?.database,
407-
name: extra?.name
408-
}
409-
this.log.canBase(message)
410-
resolve(ts)
411-
}).catch((err: any) => {
412-
console.log('send error',err)
427+
const cmdId = txCnt++
428+
429+
430+
431+
try {
432+
pendingBaseCmds.set(cmdId, { resolve, reject, msgType, id, data, extra })
433+
TOOMOSS.SendCANMsg(
434+
this.handle,
435+
this.deviceIndex,
436+
this.info.canfd,
437+
this.id,
438+
439+
cmdId,
440+
{
441+
ID: id | (msgType.idType == CAN_ID_TYPE.EXTENDED ? 0x80000000 : 0) | (msgType.remote ? 0x40000000 : 0),
442+
RemoteFlag: msgType.remote ? 1 : 0,
443+
ExternFlag: msgType.idType == CAN_ID_TYPE.EXTENDED ? 1 : 0,
444+
DataLen: data.length,
445+
Data: data,
446+
DLC: data.length,
447+
Flags: (msgType.brs ? 0x01 : 0) | (msgType.canfd ? 0x04 : 0)
448+
}
449+
)
450+
451+
452+
453+
454+
} catch (err: any) {
455+
pendingBaseCmds.delete(cmdId)
413456
reject(new CanError(CAN_ERROR_ID.CAN_INTERNAL_ERROR, msgType, data, err))
414-
})
457+
}
415458
})
416459
}
417460

src/main/docan/toomoss/swig/toomoss.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void LoadDll(const char* path) {
5858

5959
extern void CreateTSFN(const Napi::CallbackInfo &info);
6060
extern void FreeTSFN(const Napi::CallbackInfo &info);
61-
extern Napi::Promise SendCANMsg(const Napi::CallbackInfo &info);
61+
extern void SendCANMsg(const Napi::CallbackInfo& info);
6262

6363

6464
do {

src/main/docan/toomoss/swig/toomoss_wrap.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16052,7 +16052,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
1605216052

1605316053
extern void CreateTSFN(const Napi::CallbackInfo &info);
1605416054
extern void FreeTSFN(const Napi::CallbackInfo &info);
16055-
extern Napi::Promise SendCANMsg(const Napi::CallbackInfo &info);
16055+
extern void SendCANMsg(const Napi::CallbackInfo& info);
1605616056

1605716057

1605816058
do {

0 commit comments

Comments
 (0)