1# Copyright (c) (2024), Cypress Semiconductor Corporation (an Infineon company) or
2# an affiliate of Cypress Semiconductor Corporation.
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# From https://wiki.tcl-lang.org/page/constants
18proc const {name value} {
19    uplevel 1 [list set $name $value]
20    uplevel 1 [list trace var $name w {error constant;} ]
21}
22
23const ARG_IDX_IN_MAC 0
24
25const SUCCESS 0
26const ERROR_ARG_COUNT 1
27const ERROR_ID 2
28const ERROR_ARG_VALUE 3
29
30set channelName stdout
31
32if {[chan names ModusToolbox] eq "ModusToolbox"} {
33    set channelName ModusToolbox
34}
35
36proc parse_mac {} {
37    if {$::argc != $::ARG_IDX_IN_MAC + 1} {
38        error "MAC Parser requires 1 argument:
39\tstring inMACaddr - The input MAC address in the format MM:MM:MM:SS:SS:SS"
40        return $::ERROR_ARG_COUNT
41    }
42
43    set inMACaddr [lindex $::argv $::ARG_IDX_IN_MAC]
44
45    if {![regexp {([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})} $inMACaddr]} {
46        error "Unable to parse the MAC address, because it does not match the hexadecimal format MM:MM:MM:SS:SS:SS."
47        return $::ERROR_ARG_VALUE
48    }
49    set inMACaddr [split $inMACaddr ":"]
50    return [parse_mac_internal $inMACaddr]
51}
52
53proc parse_mac_internal {inMACaddr} {
54    set counter 0
55    foreach idx $inMACaddr {
56        puts $::channelName "param:macField$counter=$idx"
57        incr counter
58    }
59    return $::SUCCESS
60}
61parse_mac
62