25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

78 satır
1.7 KiB

  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  5. "github.com/AFASystems/presence/internal/pkg/model"
  6. )
  7. // ParseADFast efficiently parses Advertising Data structures
  8. // Returns slice of [startIndex, endIndex] pairs for each AD structure
  9. func ParseADFast(b []byte) [][2]int {
  10. var res [][2]int
  11. i := 0
  12. for i < len(b) {
  13. l := int(b[i])
  14. if l == 0 || i+1+l > len(b) {
  15. break
  16. }
  17. res = append(res, [2]int{i, i + 1 + l})
  18. i += 1 + l
  19. }
  20. return res
  21. }
  22. // RemoveFlagBytes removes Bluetooth advertising flag bytes if present
  23. // Some beacons include flag bytes as the first AD structure
  24. func RemoveFlagBytes(b []byte) []byte {
  25. if len(b) > 1 && b[1] == 0x01 {
  26. l := int(b[0])
  27. if 1+l <= len(b) {
  28. return b[1+l:]
  29. }
  30. }
  31. return b
  32. }
  33. // Generate event based on the Beacon type
  34. func LoopADStructures(b []byte, i [][2]int, id string, parserRegistry *model.ParserRegistry, beacon string) appcontext.BeaconEvent {
  35. be := appcontext.BeaconEvent{}
  36. for _, r := range i {
  37. ad := b[r[0]:r[1]]
  38. if !isValidADStructure(ad) {
  39. fmt.Println("invalid ad structure: ", beacon)
  40. break
  41. }
  42. for name, parser := range parserRegistry.ParserList {
  43. if parser.CanParse(ad) {
  44. event, ok := parser.Parse(name, ad)
  45. fmt.Println("beacon id: ", id)
  46. fmt.Println("parser can parse: ", name)
  47. if ok {
  48. event.ID = id
  49. event.Name = id
  50. return event
  51. }
  52. }
  53. }
  54. // fmt.Println("no parser can parse: ", beacon)
  55. }
  56. return be
  57. }
  58. // IsValidADStructure validates if an AD structure is well-formed
  59. func isValidADStructure(data []byte) bool {
  60. if len(data) < 2 {
  61. return false
  62. }
  63. length := int(data[0])
  64. return length > 0 && int(length)+1 <= len(data)
  65. }