Skip to content

Commit

Permalink
chore: convert GSM7 characters in TagMessageBox data
Browse files Browse the repository at this point in the history
  • Loading branch information
damonto committed May 29, 2024
1 parent 7808dd9 commit 7ebba19
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
5 changes: 5 additions & 0 deletions internal/cloud/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func (c *Conn) Handle(tag Tag, data []byte) {
func (c *Conn) Send(tag Tag, data []byte) error {
c.lock.Lock()
defer c.lock.Unlock()

if tag == TagMessageBox {
data = ToGSM7(data)
}

packet := c.pack(tag, data)
if tag == TagAPDU {
slog.Debug("sending data to", "id", c.Id, "tag", tag, "packet", hex.EncodeToString(packet))
Expand Down
3 changes: 1 addition & 2 deletions internal/cloud/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const (
var (
GSMNumberSign = []byte{0x23} // #
GSMDollarSign = []byte{0x02} // $
GSMUnderscore = []byte{0x11} // _
)

func handleDownloadProfile(ctx context.Context, conn *Conn, data []byte) error {
Expand Down Expand Up @@ -70,7 +69,7 @@ func download(ctx context.Context, conn *Conn, data []byte) error {
if len(parts) == 5 {
confirmationCode = string(parts[4])
if confirmationCode == "1" {
parts[4] = bytes.Replace([]byte("<confirmation_code>"), []byte("_"), GSMUnderscore, 1)
parts[4] = []byte("<confirmation_code>")
return errors.New(ErrRequireConfirmationCode + "\n" + string(bytes.Join(parts, GSMDollarSign)))
}
}
Expand Down
38 changes: 38 additions & 0 deletions internal/cloud/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,45 @@ package cloud

import "unicode"

var GSM7CharacterSet = map[rune]byte{
'@': 0x00,
'£': 0x01,
'$': 0x02,
'¥': 0x03,
'_': 0x11,
'!': 0x21,
'#': 0x23,
'%': 0x25,
'&': 0x26,
'(': 0x28,
')': 0x29,
'*': 0x2A,
'+': 0x2B,
',': 0x2C,
'-': 0x2D,
'.': 0x2E,
'/': 0x2F,
':': 0x3A,
';': 0x3B,
'<': 0x3C,
'=': 0x3D,
'>': 0x3E,
'?': 0x3F,
}

func ToTitle(s string) string {
r := []rune(s)
return string(unicode.ToUpper(r[0])) + string(r[1:])
}

func ToGSM7(b []byte) []byte {
var result []byte
for _, c := range b {
if v, ok := GSM7CharacterSet[rune(c)]; ok {
result = append(result, v)
} else {
result = append(result, c)
}
}
return result
}
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net"
"os"
"strings"
)

type Config struct {
Expand Down Expand Up @@ -44,7 +45,7 @@ func (c *Config) IsValid() error {

func (c *Config) GetAdvertising() []byte {
if c.Advertising != "" {
return []byte("!! Advertising !! \n" + c.Advertising)
return []byte("!! Advertising !! \n" + strings.Replace(c.Advertising, "_br_", "\n", -1))
}
return []byte{}
}
Expand Down

0 comments on commit 7ebba19

Please sign in to comment.